Today I learnt another important thing in Unix shell scripts.Till today I just used variables to store integer.And wil the same blur idea I was vainly trying to compare strings.My script was like :
-----
-----
-----
LATESTLOG="anything";
LATESTLOG=`ls -lrt $SCRIPT_NAME.*.log|tail -1|cut -d":" -f2|cut -d" " -f2`;
if [ $LATESTLOG -ne "anything" ];
then
echo "Do something";
else
echo "Do nothing";
fi
-----
-----
----- And the error was coming like:
<-----bad number----->Later I understood that string comparison is different in shell script and integer comparison is different.
As we know in integer comparison-eq
is equal toif [ "$a" -eq "$b" ]-ne
is not equal toif [ "$a" -ne "$b" ]-gt
is greater thanif [ "$a" -gt "$b" ]-ge
is greater than or equal toif [ "$a" -ge "$b" ]-lt
is less thanif [ "$a" -lt "$b" ]-le
is less than or equal toif [ "$a" -le "$b" ]So here the comparison was wrong.I was to compare strings which I was performing like integer comparison.
In string conparison,there are a lots of things,I will put down all those things slowly.For now the basic string comparison is like:
is equal toif [ "$a" = "$b" ]is not equal toif [ "$a" != "$b" ]So the rectified portion of my script became:
-----
-----
-----
LATESTLOG="anything";
LATESTLOG=`ls -lrt $SCRIPT_NAME.*.log|tail -1|cut -d":" -f2|cut -d" " -f2`;
if [ $LATESTLOG != "anything" ];
then
echo "Do something";
else
echo "Do nothing";
fi
-----
-----
-----Labels: Technical_HowTo_Unix