views:

54

answers:

3

Hi all, I have been trying to execute the following unix shell script which is not working. I am running it by ksh.

echo $?;
if [ $? -ne 0 ]
then
 failed $LINENO-2 $5 $6
fi
failed()
{
        echo "$0 failed at line number $1";
 echo "moving $2 to failed folder"
}

This is giving an error saying "Synatx error:then unexpected". Basically I have to check for the last executed korn shell script's highest/last statement's return code and if it is not equal to zero i have to call function failed with the given parameters. I tried putting semicolon before then that also dint work out.

Can you please help?

Edit: Based on the inputs i changed code. still the same problem exists.

ksh ../prescript/Pre_process $1 $2 $3
rc=$?;
if [[ $rc -ne 0 ]];then
    echo "failed";
       exit 1;

Edit2: Hi all, its working for the then part by using double squared brackets. I feel i used code of bash script for Ksh. but i am facing problem in function call of failed. Please let me know appropriate way of function call in korn shell for this example

A: 

you are missing semicolons at the end of the lines:

if [ $? -ne 0]; then
   # …
knittl
That's only if you put `then` on the same line. Putting it on a separate line is fine.
paxdiablo
yeah. I kept ; before then as shown above then it didnt work so i went back to new line then
Enjoy coding
+2  A: 

You need to be careful. The first operation on $? will usually clear it so that your if won't work anyway.

You would be better off using:

rc=$?
echo $rc
if [ $rc -ne 0 ]
:

Other than that, it works fine for me:

$ grep 1 /dev/null

$ if [ $? -ne 0 ]
> then
> echo xx
> fi
xx

$ grep 1 /dev/null

$ echo $?;
1

$ if [ $? -ne 0 ]
> then
> echo yy
> fi
$ _

Note the lack of output in the last one. That's because the echo has sucked up the return value and overwritten it (since the echo was successful).

As an aside, you should let us know which UNIX and which ksh you're actually using. My working version is ksh93 under Ubuntu. Your mileage may vary if you're using a lesser version.


It looks like, from your update, your only problem now is the function call. That's most likely because you're defining it after using it. The script:

grep 1 /dev/null
rc=$?
if [ $rc -ne 0 ]
then
        failed $rc
fi

failed()
{
        echo Return code was $1
}

produces:

qq.ksh[6]: failed: not found

while:

failed()
{
        echo Return code was $1
}

grep 1 /dev/null
rc=$?
if [ $rc -ne 0 ]
then
        failed $rc
fi

produces

Return code was 1
paxdiablo
I checked that but atleast for getting in the zero value as $? i used echo on top. Still my problem is then not working. Is there any thing to do for making it work on ksh
Enjoy coding
A: 

This looks like bash rather than ksh

failed() {  
  echo "$0 failed at line number $1";  
  echo "moving $2 to failed folder"  
}

if [[ $? -ne 0 ]]
then
  failed $LINENO-2 $5 $6  
fi
Jon Freedman
can you please let me know how can i make it work on korn shell.
Enjoy coding
Use double square brackets
Jon Freedman
thanks its working for then part but i am facing different problem with "failed" function call. can you please tell me korn shell way of calling function
Enjoy coding
You need to define the function before calling it - edited reply
Jon Freedman