Shell script: if statement

I follow the tutorial here: http://bash.cyberciti.biz/guide/If..else..fi#Number_Testing_Script

My script looks like this:

lines=`wc -l $var/customize/script.php` if test $lines -le 10 then echo "script has less than 10 lines" else echo "script has more than 10 lines" fi 

but my conclusion is like this:

 ./boot.sh: line 33: test: too many arguments script has more than 10 lines 

Why does he say that I have too many arguments? I do not see how my script is different from the one in the tutorial.

+6
source share
3 answers
Command

wc -l file print two words. Try the following:

 lines=`wc -l file | awk '{print $1}'` 

To debug a bash script (boot.sh) you can:

 $ bash -x ./boot.sh 

It will print every line executed.

+10
source
 wc -l file 

exits

 1234 file 

using

 lines=`wc -l < file` 

to get only the number of rows. In addition, some people prefer this notation instead of backlinks:

 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: $fn elif test $(wc -l < "$fn") -le 10 then echo less than 11 lines else echo more than 10 lines fi 
+8
source

In addition, you should use

 if [[ $lines -gt 10 ]]; then something else something fi 

test condition really outdated, and therefore it is the direct successor to [ condition ] , mainly because you have to be very careful with these forms. For example, you must specify any $var that you pass to test or [ ] , and there are other details that get hairy. (tests are processed in all ways, like any other command). See the article for more details.

+1
source

Source: https://habr.com/ru/post/917863/


All Articles