How To In Unix:String Comparison In Unix...
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 to
if [ "$a" -eq "$b" ]
-ne
is not equal to
if [ "$a" -ne "$b" ]
-gt
is greater than
if [ "$a" -gt "$b" ]
-ge
is greater than or equal to
if [ "$a" -ge "$b" ]
-lt
is less than
if [ "$a" -lt "$b" ]
-le
is less than or equal to
if [ "$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 to
if [ "$a" = "$b" ]
is not equal to
if [ "$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
0 Comments:
Post a Comment
<< Home