whla
whla

Reputation: 811

Illegal number in shell script

I am new to writing scripts and am trying to start out with a simple one. I am stumped as to why I am receiving the error: [: 13: Illegal number: count from the code below. Line 13 is the last fi.

count=grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/****/zlsapp.log | wc -l
if [ count -ge 50 ]
then
    if [ count -lt 100 ]
    then
        exit 1
    fi  
    if [ count -ge 100 ]
    then 
        exit 2
    fi
    exit 0
fi

Also is there anyway to do compound if statements like if(count >= 50 && count < 100)?

Upvotes: 3

Views: 11482

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263557

The problem is that count does not refer to the variable count; it's simply the string "count".

Change:

if [ count -ge 50 ]

to

if [ $count -ge 50 ]

and make the corresponding change elsewhere (but not in the initial assignment).

You should also use double quotes:

if [ "$count" -ge 50 ]

Also is there anyway to do compound if statements like if(count >= 50 && count < 100)?

Yes:

if [ "$count" -ge 50 -a "$count" -lt 100 ]

is likely to work, but the -a (logical and) operator is marked as obsolescent by POSIX. Instead write

if [ "$count" -ge 50 ] && [ "$count" -lt 100 ]

If you're using bash, info bash and search for the "test" command ([ is an alias for test).

And if you're using bash, you should consider using [[ ... ]] rather than [ ... ] -- or you can use (( ... )) for arithmetic expressions. See the bash documentation for more information (follow the iink or type info bash).

In addition to the missing $ signs, the first line of your script:

count=grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/****/zlsapp.log | wc -l

doesn't set the count variable, since you're not capturing the output of the grep ... | wc -l command. To do so:

count=$(grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/****/zlsapp.log | wc -l)

(Yes, $(...) can be nested.)

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754710

The line:

count=grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/zumigo/zlsapp.log | wc -l

probably does not do what you think it does (or you've not accurately copied and pasted your actual code into the question). If run in the middle of 2014-08-01, it runs the command "2014-08-01 12:00" with the log file as an argument and the environment variable count set to the value grep, and pipes the output from the probably non-existent command to wc -l.

When you subsequently go to test $count in the test statements, it is an empty string, which doesn't convert properly to a number.

You probably meant:

count=$(grep "^$(date -d -30minute +'%Y-%m-%d %H:%M')" /var/log/zumigo/zlsapp.log | wc -l)

This captures the output of running grep on the log file and piping the output to wc -l.

If you run your script with bash -x or equivalent (the -x option usually shows the execution of the script), you should see this.

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84599

Two reasons. (1) in bash variables are untyped (could be int, could be char). In order to remove ambiguity, you can specify the type with:

declare -i count

To tell bash it should be an int. (2) you need to dereference your variables with $ to get the number back. I.E.

[ $count -lt 100 ]

(it is also good practice to quote your variables - not required, but good practice: [ "$count" -lt 100 ]. Drop a comment if you still have trouble.

Upvotes: 5

Related Questions