Hi
How I can setto variable named MYINT in bash script and then use it in if statement e.g.Code:grep 'TEST' ~/MY.TEST -c -w
Code:if [ $MYINT = 0 ] then echo 'varianle is null' else echo $MYINT fi
Hi
How I can setto variable named MYINT in bash script and then use it in if statement e.g.Code:grep 'TEST' ~/MY.TEST -c -w
Code:if [ $MYINT = 0 ] then echo 'varianle is null' else echo $MYINT fi
Last edited by Waappu; January 30th, 2007 at 12:06 AM.
Regards,
Jari
MYINT=`grep 'TEST' ~/MY.TEST -c -w`
-- Guilherme H. Polo Goncalves <ggpolo@gmail.com>
Hi
Thank you =)
Regards,
Jari
gpolo's code is correct. Note carefully that the statement is surrouding not in normal quotes but in backtick characters (usually found on the upper-left of the keyboard). Surrounding a bash command with backtick characters means that the shell will execute the command and turn the return value into a string you can use elsewhere.
Hope that helps.
Hi
Yes this help. I some how missed that backtick.
Thanks to both of you
Regards,
Jari
Waappu,
The special variable '$?' contains the exit code of the last run command. You could also use this instead of assigning to a new variable if you wish. This works from the command line as well as from a script.
when grep runs, it returns 2 things, output and an exit code.
consider the following:
the first line will assign any output from grep to result. if grep output anything, result will be not empty. the exitcode variable will contain the exit code from the previous command, if grep ran perfectly (whether it found anything or not), exitcode will be 0.Code:result = $(grep "TEST" ~/mytest -c -w) exitcode $?
Waappu,
The end result is the same, I was just letting you know.Code:grep 'TEST' ~/MY.TEST -c -w if [ $? = 0 ]; then echo 'variable is zero' else echo $? fi
BTW, null and zero are two different things.
Hi
Thanks very much. Now I get it =)
Also find answer/my mistake to other queston
Regards,
Jari
Bookmarks