bash - Trouble nesting expressions in UNIX -
i have following unix statement:
#!/bin/bash $x=$((grep ^'[a-z]' $1 | wc -l)) echo "$x"
however, i'm getting error message relating missing operand whenever try run script. there way assign variable value in unix?
edit:
well, became clear me grep cannot seem examine single words, intended do. there unix command can point me deals searching pattern in word? i'm trying make unix code can tell if word passed script starts number. unix call helpful?
i believe remaining problem $( ... )
, $(( ... ))
2 different things. former command substitution, want. latter arithmetic. try this:
#! /bin/sh x=$(grep -c '^[a-z]' "$1") echo "$x"
i don't know why had caret outside single quotes; cause problems shells (where caret either history expansion or alias |), think bash not 1 of shells there's no reason tempt fate.
obligatory tangential advice: when expanding shell variables, put them inside double quotes, unless need word splitting happen. (this why changed $1
"$1"
. without change, script break if gave file space in name.)
edit: want know if word passed script starts number. "passed script" going assume mean this:
./script word
the easiest way not involve grep
or command substitution @ all:
#! /bin/sh case "$1" in [0-9]*) echo "starts number" ;; *) echo "doesn't start number" ;; esac
yes, "case" syntax gratuitously different else in shell; if want internally consistent language, python waiting thataway. patterns put before each open paren globs, not regexes. if need regex, have more complicated, e.g.
#! /bin/sh if [ $(expr "$1" : '[0-9]\{3,5\}x') -gt 0 ]; echo "starts three-to-five digit number followed x" fi
some people unnecessary put quotes around variable expansion goes between case
, in
. technically correct should ignored anyway, because that's 1 many warts remember. other people can put open parenthesis before each glob , editor not grumpy mismatched parentheses. approve of editor non-grumpiness, don't know how portable variation is.
Comments
Post a Comment