shell script: if statement
wc -l file
command will print two words. Try this:
lines=`wc -l file | awk '{print $1}'`
To debug a bash script (boot.sh), you can:
$ bash -x ./boot.sh
It'll print every line executed.
wc -l file
outputs
1234 file
use
lines=`wc -l < file`
to get just the number of lines. Also, some people prefer this notation instead of backticks:
lines=$(wc -l < file)
Also, since we don't know if $var
contains spaces, and if the file exists:
fn="$var/customize/script.php"if test ! -f "$fn"then echo file not found: $fnelif test $(wc -l < "$fn") -le 10then echo less than 11 lineselse echo more than 10 linesfi
Also, you should use
if [[ $lines -gt 10 ]]; then somethingelse somethingfi
test condition
is really outdated, and so is it's immediate successor, [ condition ]
, mainly because you have to be really careful with those forms. For example, you must quote any $var
you pass to test
or [ ]
, and there are other details that get hairy. (tests are treated in every way as any other command). Check out this article for some details.