Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script with wc -l, if statement ain't working

Tags:

bash

shell

The problem is with this code:

    words=`wc -l /home/tmp/logged.log | awk '{print $1}'`;
    if [ $words == 26 ]
    then
    echo $words
    echo Good
    else
    echo Not so good
    fi

it always returns the else statement. Even tho the result is 26. I also tried

     words=`wc -l < /home/jonathan/tmp/logged.log`;
like image 565
user1204032 Avatar asked Feb 24 '12 12:02

user1204032


2 Answers

try to use [ $words -eq 26 ] instead of [ $words == 26 ]

or [ 26 == 26 ] to check that statement works properly

like image 59
triclosan Avatar answered Nov 04 '22 13:11

triclosan


Because == is not valid. Use =

if [ $words = 26 ]

By the way you can use cut instead of awk.

wc -l /home/tmp/logged.log  | cut -f1 -d" "
like image 3
Shiplu Mokaddim Avatar answered Nov 04 '22 12:11

Shiplu Mokaddim