grep with if problem

#!/bin/ksh
a =`grep MAJOR filename | tail`
if [ $a -eq 0 ]
echo "    "
else
echo " $a "
fi

filename is like alarmlog file which had alarms in it i am trying to grep the mejor alrms from the file.if there are any major alarms i have to print them else nothing.
its giving me a syntax error.

./p.sh[3]: a:  not found
./p.sh[5]: syntax error at line 10 : `else' unexpected

Hi
2 things:

  1. You are missing 'then' after if.
if [ $a -eq 0 ]; then
echo " "
else
  1. Are you sure $a is a number since you are using -eq. Because from the grep it looks like it cannot be a number.

Guru.

1 Like

Remove the space between the variable `a' and the `='
The `if' statement is missing the `then'
Remove the spaces between " and variable a in `echo " $a "'

hi its not a number its an alarm file i just grep the alarms and i am still having the problem

./w.sh[3]: 2:  not found
./w.sh[5]: Alarms:  not found
#!/bin/ksh

Alarms= $(grep MAJOR file/alarm.log* | tail|wc -l)

if [ "$(Alarms)" -eq 0 ];

then

echo "    "

else

echo " $Alarms "

fi

There can not be spaces in the assignment

lvalue=rvalue
"$(Alarms)"

The quotes are recommended, the parenthesis are doing nothing.

| tail|wc -l)

tail displays ten lines by default if exist in the file, therefore wc -l return a 10 line or less, always, even if there are more.
tail is not necessary there.

echo " $Alarms "

Are you aware you're adding literal spaces in front and after any value that $Alarms holds?

thank you for reply but i tried this as well it gives me an error

a=`grep MAJOR /file../../alarm.log* | wc `
if [ $a != "  " ]
 then
   echo ' $a '

fi
./f1.sh[4]: :=: unknown test operator

---------- Post updated at 01:40 AM ---------- Previous update was at 01:31 AM ----------

i have used code tags this is not whats in the original file i have used code tags after the first warning.if you want i can poaste exactly whats in the original file.should i be doing that to prve u wrong

A few things you might want to know about ksh
Explanation from the past, about the `test' in the ksh shell

ksh basics

Since you have posted many pieces, all with errors, I'll give you an example

#! /bin/ksh

alarms=$(grep MAJOR alarm.log | wc -l)

if [[ $alarm -le 0 ]]; then
    echo "Nothing to report"
else
    echo "$alarms to report"
fi
1 Like