Operating System - HP-UX
1753538 Members
4573 Online
108795 Solutions
New Discussion

Re: ksh: error handling with a sub shell-script

 
SOLVED
Go to solution
support_billa
Valued Contributor

ksh: error handling with a sub shell-script

hello,

 

following issue: i want to start from a script a lot of sub-shell-scripts and want to handle in the main script , if an error occurs in the sub script. set -e has no impact, when EXOR "||" or AND "&&" exist in a command.

 

i want to handle in the main script , if a error occurs in the sub script.

in the example i make a "ls" of an not existing file .

 

example:


Main-Script :  /tmp/ksh/main.sh

BEGIN BEGIN
#!/usr/bin/ksh

set -x
set -e
/tmp/ksh/sub.sh
echo $?
END END


Sub-Script /tmp/ksh/sub.sh

BEGIN BEGIN
#!/usr/bin/ksh
ls /tmp/notfound  || echo "Error"
END END


Version 1:

I can't get the error , because of "||"

Output
xxxxxxxxxxxxxxxxxxx
./main.sh
+ set -e
+ /tmp/ksh/sub.sh
/tmp/notfound not found
Error
+ echo 0
0
xxxxxxxxxxxxxxxxxxx

 

Version 2:  ( change in sub.sh)

Remove "||"

Change: ls /tmp/notfound  || echo "Error"
To    : ls /tmp/notfound

the main script stops, but also i can't get the error ....

Output

xxxxxxxxxxxxxxxxxxx
+ set -e
+ /tmp/ksh/sub.sh
/tmp/notfound not found
xxxxxxxxxxxxxxxxxxx

 

kind regards

8 REPLIES 8
Matti_Kurkela
Honored Contributor

Re: ksh: error handling with a sub shell-script

There are two ways.

You could have your sub-script store the result code of the 'ls' command and pass it to the calling script:

#!/usr/bin/ksh
ls /tmp/notfound
RESULT=$?
if [ $RESULT -ne 0 ]; then
    echo "Error"
fi
return $RESULT

 

Alternatively, you could have your sub-script return result codes of your own choosing:

 

#!/usr/bin/ksh
# Result code scheme:
# 0 = OK (standard)
# 1 = ls /tmp/notfound failed
# 2 = something else failed
# ...
ls /tmp/notfound
if [ $? -ne 0 ]
then
    echo "Error"
    return 1
fi
return 0

 

Note: Because $? refers to the result code of the immediately preceding command line, if you want to do something

within the sub-script after the possibly-failing command and then make decisions based on the result code, you must store the result code to a different variable *immediately* after the command you're interested in. Otherwise the

result code gets overwritten by the subsequent commands: for example, in your original sub-script, the || detects the non-zero result code from 'ls' and runs the echo command, which is successful and so sets the result code to 0.

 

If the main script only wants to know if the sub-script was successful or not, you can use the first method. But if the sub-script does several things and the main script needs to do different things depending on *which of them* failed in the sub-script, you'd better use the second method.

MK
Dennis Handly
Acclaimed Contributor

Re: ksh: error handling with a sub shell-script

>when EXOR "||"

 

This is inclusive OR, not XOR.

 

>ls /tmp/notfound  || echo "Error"

 

If you are just checking for existence, you probably don't want to print the name of the file or the error.

You could check with:

if [ -f /tmp/notfound ]; then

   echo "Error"

   return 1

fi

 

Or just redirect stdout and stderr:

ls /tmp/notfound  > /dev/null 2>&1

support_billa
Valued Contributor

Re: ksh: error handling with a sub shell-script

hello,

 

thank you for your responses.

summary : i have to change my sub - scripts.

 

a typical application of us :

 

we create with scripts  filesystems like ( put the filesystem config in a config file and create several scripts) :

 

1. script:  lvcreate

2. script: newfs

 

1. script :

lvcreate -L .......   || echo "LINE1 Error of lvcreate ... "

lvcreate -L .......   || echo "LINE2 Error of lvcreate ... "

 

2. script:

newfs    .... || echo "LINE1 Error of newfs ...."

 

those script we start manually ( so i see the error) . i want to start in a main script those sub-scripts and i have to change the sub-scripts and want trust all the scripts ( i mean error handling)

right  ?

 

when in one sub-script an error occurs, i can't get the error.

 

regards
'

Dennis Handly
Acclaimed Contributor
Solution

Re: ksh: error handling with a sub shell-script

>when in one sub-script an error occurs, I can't get the error.

 

You can always change your lines to:

lvcreate -L .......   || (echo "LINE1 Error of lvcreate ... ";  exit 1)

support_billa
Valued Contributor

Re: ksh: error handling with a sub shell-script

hello,

 

thank you for great input , this help me tremendous,

because i only know this syntax

 

 

ls /tmp/notfound || { echo "Error"                       

                                       exit 1                           

                                     }              

 


and with this syntax makes it not "easy to read or confusing" . lvcreate should be "a one liner" and stops , when a error occurs.

 

thank you very much

 

kind regards,tom 

Dennis Handly
Acclaimed Contributor

Re: ksh: error handling with a sub shell-script

>because I only know this syntax

 

Ah, that's better, it doesn't create a subshell.  You can put it all in one line if you want:
ls /tmp/notfound || { echo "Error";  exit 1;  }              

 

support_billa
Valued Contributor

Re: ksh: error handling with a sub shell-script

hello,

 

about

 

ls /tmp/notfound || { echo "Error";  exit 1;  }

 

it's a little bit tricky : 

allowed:

ls /tmp/notfound || { echo "Error";  exit 1;  }    

when you leave ";" , it is a syntax error

not allowed:

ls /tmp/notfound || { echo "Error";  exit 1 }

 

regards,tom

Dennis Handly
Acclaimed Contributor

Re: ksh: error handling with a sub shell-script

>when you leave ";" , it is a syntax error

 

Exactly, that's why I told you to put it there.  If you look very closely at ksh(1), you see a ";" there:

   { list ;}