1834818 Members
2792 Online
110070 Solutions
New Discussion

script error

 
SOLVED
Go to solution
Penny Patch
Occasional Advisor

script error

I have written a script which takes if the previous command ran successfully or not. If the previous command ran successfully,then excecute the next command.

#!/bin/ksh
ls -l
if [ `echo $?` == 0 ]
then
echo "success"

else
echo "Failed"
fi

But its giving an error,
==: 0403-012 A test command parameter is not valid.

any ideas ..why?
thanks.
5 REPLIES 5
Amin Jaffer_1
Occasional Advisor
Solution

Re: script error

Change the code to following...

ls -l
if [ $? -eq 0 ]
then
echo "success"
else
echo "Failed"
fi

To compare i think you would need to use
-eq, -gt, -lt, -le... to compare integers

i don't think there is a comparison operator "==" for integer
I.Delic
Super Advisor

Re: script error

thry this

ls -l
a=`echo $?` ---or--- a=$(echo $?)
if [ "$a" -eq 0 ]
then
echo scucces
else
echo faild
fi


succes

idriz






curt larson_1
Honored Contributor

Re: script error

the == is arithmetic operator and your using the obsoleted [ ]. use the (( )) syntax for and you won't get the error. But it still won't work.

first of all the tic '..' are obsolete too. use $(..) instead.

$? is the return code of the last command. I would guess that your expecting a 0 for success. So, what your doing is running the command echo 0, take it's return code (which should be 0 as long as you can write to stdout), and then test if it is the value 0.
so what your actually testing is if echo $? was successfull and not the ls.

probably what your wanting to do is
if [[ "$?" = "0" ]] ;then
#success
.
.
or
if (( $? = 0 )) ;then
#success
.
.
.
Penny Patch
Occasional Advisor

Re: script error

Thanks Amin. That solved the
Penny Patch
Occasional Advisor

Re: script error

Thanks all.