Unix/Linux OS/[[ ]], (( )), [ ] in Korn shell
Expert: Donny Forbes - 5/20/2004
QuestionThanks Donny for your comprehensive reply. It is very much appreciated. Please look at the following Korn shell script that uses [ ] and (( )):
#$Id: pascc,v 1.1 2004/04/12 02:46:08 ymalik Exp $
#!/bin/ksh
java -cp ../lexer:$CLASSPATH parser $1 $2 $3 $4 $5 $6 $7;
ret=$?;
if (( $ret != 0 )) then
#if [ $ret -ne 0 ]; then
rm ${2:-obj.c}
else
gcc -o exec ${2:-obj.c};
fi
exit $ret;
The java call returns 0 on success and 1 on error. If there is an error, I am supposed to execute the "then" portion of the "if" statement and on success, the "else" portion is executed. If I run the above script as is I get the following message:
1: not found
<and the call to gcc is executed>
which is not what I want since when java returns 1, the "then" portion should be executed. If the java call returns 0, the "else" portion is executed--which is what I want--but the following message comes:
0: not found
However, if I comment out the "if" with (( )) but use the "if" with [ ], everything goes fine. Why isn't (( )) working for arithmetic operations in this case?
Thanks,
Yasir
AnswerHello Yasir,
Not sure if the problem has to do with (( )) try adding this to the top of your script.
set -vx cmd 2>errlog
Run your script in the same directory as you know it will create a errlog file . Lets look at that see if you see anything if you want email it to df_forbes@hotmail.com
Thanks.
DF