Monday 11 April 2011

argument expected error in a shell script


[: argument expected

I was getting the above error while creating a shell script like for example:

for datos in `ls -rt $UNXLOG/26-Jan*`
do
export arch=`echo $datos |cut -d, -f1`
if [ `grep -c INACTIVO ${arch}` -eq 0 ]
then
export linea1=`grep Debut ${arch}`
export horatot=`echo $linea1 |cut -d' ' -f5`
export hora=`echo $horatot |cut -c1-2`

if [ ${hora} -le 19 ]
then
echo "Listando log - ${arch} ...\n" >> $UNXLOG/qq.log
more ${arch} >>$UNXLOG/qq.log
echo "--------------------------------------------------------------------------------" >>$UNXLOG/qq.log
fi
fi
done


The problem is that hora has no value at all. Put a
echo hora = $hora
in front of the if statement to see that. This means that the if statement is just:
Code:
if [       -le 19 ]

One solution is to expand hora with a default value:

if [ ${hora:-1} -le 19 ]

Now if hora is unset or set to null, the if statement will see 1.


References:

http://www.unix.com/unix-dummies-questions-answers/16696-test-argument-expected-error.html  { accessed on apr 11 2011 }

4 comments: