<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ksh: error handling with a sub shell-script in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831483#M638697</link>
    <description>&lt;P&gt;There are two ways.&lt;/P&gt;&lt;P&gt;You could have your sub-script store the result code of the 'ls' command and pass it to the calling script:&lt;/P&gt;&lt;PRE&gt;#!/usr/bin/ksh
ls /tmp/notfound
RESULT=$?
if [ $RESULT -ne 0 ]; then
    echo "Error"
fi
return $RESULT&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, you could have your sub-script return result codes of your own choosing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#!/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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: Because $? refers to the result code of the immediately preceding command line, if you want to do something&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Fri, 12 Oct 2012 13:24:58 GMT</pubDate>
    <dc:creator>Matti_Kurkela</dc:creator>
    <dc:date>2012-10-12T13:24:58Z</dc:date>
    <item>
      <title>ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831361#M638696</link>
      <description>&lt;P&gt;hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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 "&amp;amp;&amp;amp;" exist in a command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want to handle in the main script , if a error occurs in the sub script.&lt;/P&gt;&lt;P&gt;in the example i make a "ls" of an not existing file .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Main-Script :&amp;nbsp; /tmp/ksh/main.sh&lt;/P&gt;&lt;P&gt;BEGIN BEGIN&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;set -x&lt;BR /&gt;set -e&lt;BR /&gt;/tmp/ksh/sub.sh&lt;BR /&gt;echo $?&lt;BR /&gt;END END&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Sub-Script /tmp/ksh/sub.sh&lt;/P&gt;&lt;P&gt;BEGIN BEGIN&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;ls /tmp/notfound&amp;nbsp; || echo "Error"&lt;BR /&gt;END END&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Version 1:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;I can't get the error , because of "||"&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Output&lt;BR /&gt;xxxxxxxxxxxxxxxxxxx&lt;BR /&gt;./main.sh&lt;BR /&gt;+ set -e&lt;BR /&gt;+ /tmp/ksh/sub.sh&lt;BR /&gt;/tmp/notfound not found&lt;BR /&gt;Error&lt;BR /&gt;&lt;STRONG&gt;+ echo 0&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;BR /&gt;xxxxxxxxxxxxxxxxxxx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Version 2:&amp;nbsp; ( change in sub.sh)&lt;/P&gt;&lt;P&gt;Remove "||"&lt;/P&gt;&lt;P&gt;Change: ls /tmp/notfound&amp;nbsp; || echo "Error"&lt;BR /&gt;To&amp;nbsp;&amp;nbsp;&amp;nbsp; : ls /tmp/notfound&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;the main script stops, but also i can't get the error ....&lt;BR /&gt;&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Output&lt;/P&gt;&lt;P&gt;xxxxxxxxxxxxxxxxxxx&lt;BR /&gt;+ set -e&lt;BR /&gt;+ /tmp/ksh/sub.sh&lt;BR /&gt;/tmp/notfound not found&lt;BR /&gt;xxxxxxxxxxxxxxxxxxx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;kind regards&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2012 11:42:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831361#M638696</guid>
      <dc:creator>support_billa</dc:creator>
      <dc:date>2012-10-12T11:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831483#M638697</link>
      <description>&lt;P&gt;There are two ways.&lt;/P&gt;&lt;P&gt;You could have your sub-script store the result code of the 'ls' command and pass it to the calling script:&lt;/P&gt;&lt;PRE&gt;#!/usr/bin/ksh
ls /tmp/notfound
RESULT=$?
if [ $RESULT -ne 0 ]; then
    echo "Error"
fi
return $RESULT&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, you could have your sub-script return result codes of your own choosing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#!/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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: Because $? refers to the result code of the immediately preceding command line, if you want to do something&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2012 13:24:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831483#M638697</guid>
      <dc:creator>Matti_Kurkela</dc:creator>
      <dc:date>2012-10-12T13:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831881#M638698</link>
      <description>&lt;P&gt;&amp;gt;when EXOR "||"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is inclusive OR, not XOR.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;gt;ls /tmp/notfound&amp;nbsp; || echo "Error"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you are just checking for existence, you probably don't want to print the name of the file or the error.&lt;/P&gt;&lt;P&gt;You could check with:&lt;/P&gt;&lt;P&gt;if [ -f /tmp/notfound ]; then&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; echo "Error"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; return 1&lt;/P&gt;&lt;P&gt;fi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or just redirect stdout and stderr:&lt;/P&gt;&lt;P&gt;ls /tmp/notfound&amp;nbsp; &amp;gt; /dev/null 2&amp;gt;&amp;amp;1&lt;/P&gt;</description>
      <pubDate>Sat, 13 Oct 2012 00:12:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831881#M638698</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2012-10-13T00:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831909#M638699</link>
      <description>&lt;P&gt;hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for your responses.&lt;/P&gt;&lt;P&gt;summary : i have to change my sub - scripts.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;a typical application of us :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;we create with scripts&amp;nbsp; filesystems like ( put the filesystem config in a config file and create several scripts) :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. script:&amp;nbsp; lvcreate&lt;/P&gt;&lt;P&gt;2. script: newfs&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. script :&lt;/P&gt;&lt;P&gt;lvcreate -L .......&amp;nbsp;&amp;nbsp; || echo "LINE1 Error of lvcreate ... "&lt;/P&gt;&lt;P&gt;lvcreate -L .......&amp;nbsp;&amp;nbsp; || echo "LINE2 Error of lvcreate ... "&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. script:&lt;/P&gt;&lt;P&gt;newfs&amp;nbsp;&amp;nbsp;&amp;nbsp; .... || echo "LINE1 Error of newfs ...."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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)&lt;/P&gt;&lt;P&gt;right&amp;nbsp; ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when in one sub-script an error occurs, i can't get the error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards&lt;BR /&gt;'&lt;/P&gt;</description>
      <pubDate>Sat, 13 Oct 2012 07:05:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831909#M638699</guid>
      <dc:creator>support_billa</dc:creator>
      <dc:date>2012-10-13T07:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831913#M638700</link>
      <description>&lt;P&gt;&amp;gt;when in one sub-script an error occurs, I can't get the error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can always change your lines to:&lt;/P&gt;&lt;P&gt;lvcreate -L .......&amp;nbsp;&amp;nbsp; || (echo "LINE1 Error of lvcreate ... ";&amp;nbsp; exit 1)&lt;/P&gt;</description>
      <pubDate>Sat, 13 Oct 2012 07:52:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5831913#M638700</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2012-10-13T07:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5832251#M638701</link>
      <description>&lt;P&gt;hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for great input , this help me tremendous,&lt;/P&gt;&lt;P&gt;because i only know this syntax&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ls /tmp/notfound || { echo "Error"&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;and with this syntax makes it not "easy to read or confusing" . lvcreate should be "a one liner" and stops , when a error occurs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you very much&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;kind regards,tom&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Oct 2012 09:40:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5832251#M638701</guid>
      <dc:creator>support_billa</dc:creator>
      <dc:date>2012-10-14T09:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5832253#M638702</link>
      <description>&lt;P&gt;&amp;gt;because I only know this syntax&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ah, that's better, it doesn't create a subshell.&amp;nbsp; You can put it all in one line if you want:&lt;BR /&gt;ls /tmp/notfound || { echo "Error";&amp;nbsp; exit 1;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Oct 2012 09:45:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5832253#M638702</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2012-10-14T09:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5833337#M638703</link>
      <description>&lt;P&gt;hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;about&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ls /tmp/notfound || { echo "Error";  exit 1;  }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it's a little bit tricky :&amp;nbsp;&lt;/P&gt;&lt;P&gt;allowed:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ls /tmp/notfound || { echo "Error";&amp;nbsp; exit 1;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;when you leave ";" , it is a syntax error&lt;/P&gt;&lt;P&gt;not allowed:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ls /tmp/notfound || { echo "Error";&amp;nbsp; exit 1 }&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;regards,tom&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2012 07:29:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5833337#M638703</guid>
      <dc:creator>support_billa</dc:creator>
      <dc:date>2012-10-15T07:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: ksh: error handling with a sub shell-script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5833693#M638704</link>
      <description>&lt;P&gt;&amp;gt;when you leave ";" , it is a syntax error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Exactly, that's why I told you to put it there.&amp;nbsp; If you look very closely at ksh(1), you see a ";" there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; { list ;}&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2012 10:56:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/ksh-error-handling-with-a-sub-shell-script/m-p/5833693#M638704</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2012-10-15T10:56:30Z</dc:date>
    </item>
  </channel>
</rss>

