Reputation: 125
How do I get the exit code from my subscript in main script. In my script below when the subscript fails it exits from the main script as well.
#!/bin/bash
function reportError() {
if [ $1 -ne 0 ]; then
echo $2
exit $1
fi
}
#executing the subscript
/data/utility/testFolder.sh
#calling the function if there is any error then function would update the
#audit table
reportError $? "job is failed, please check the log for details"
Subscript code -
#!/bin/bash
if [ -d "/data/myfolder/testfolder" ]
then
echo "ERROR: Directory does not exists"
exit 1
else
echo "INFO: Directory exists"
exit 0
fi
Upvotes: 3
Views: 3158
Reputation: 2135
I checked your code and everything is fine except you made a mistake in the subscript condition.
#!/bin/bash
function reportError() {
if [ $1 -ne 0 ]; then
echo $2
exit $1
fi
}
#executing the subscript
/data/utility/testFolder.sh
#calling the function if there is any error then function would update the
#audit table
reportError $? "job is failed, please check the log for details"
Child script:
#!/bin/bash
if [ ! -d "/data/myfolder/testfolder" ] # add "!". More details: man test
then
echo "ERROR: Directory does not exists"
exit 1
else
echo "INFO: Directory exists"
exit 0
fi
Upvotes: 1