- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ksh: error handling with a sub shell-script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2012 04:42 AM
10-12-2012 04:42 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2012 06:24 AM
10-12-2012 06:24 AM
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.
- Tags:
- exit status
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2012 05:12 PM
10-12-2012 05:12 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2012 12:05 AM
10-13-2012 12:05 AM
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
'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2012 12:52 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2012 02:40 AM
10-14-2012 02:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2012 02:45 AM
10-14-2012 02:45 AM
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; }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2012 12:29 AM
10-15-2012 12:29 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2012 03:56 AM
10-15-2012 03:56 AM
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 ;}