Reputation: 548
I have 2 scripts . I'm invoking one script from the other for capturing the exit status.
Import.sh
SCHEMA=$1
DBNAME=$2
LOGPATH=/app/dbimport/PreImport_`date +%d%b%Y`.log
export ORACLE_HOME=/oracle/product/11.2.0/db
set -x
for line in `cat "$SCHEMA" | egrep -w 'PANV|PANVPXE'`
do
USER=`echo "$line" |cut -d ';' -f1`
echo "Fetching User : $USER" >> "$LOGPATH"
PASSWORD=`echo "$line" | cut -d ';' -f2`
echo "Fetching Password: $PASSWORD" >> "$LOGPATH"
SOURCE=`echo "$line" | cut -d ';' -f3`
echo "Fetching Source Schema : $SOURCE" >> "$LOGPATH"
done
exit $?
temp.sh
RC=`/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA`
echo "Return code = $RC"
schema_remap_AANV02_UAT2.txt
AANVPXE;Arju4578;PANVPXE
AANVSL;Arj0098;PANVSL
AANV;Arju1345;PANV
the .txt file does not have read permission(make sure that you do not give read permission), so the script should fail by returning the exit status as exit $? .
Below is the output after i run temp.sh
+ cat schema_remap_AANV02_UAT2.txt
+ egrep -w 'PANV|PANVPXE'
cat: schema_remap_AANV02_UAT2.txt: Permission denied
+ exit 1
Return code =
Internal scripts is exiting with exit 1(since cat command is failing) , but inside temp.sh i'm not getting the expected value while capturing the return code.
I want to make sure that whichever command fails in import.sh , the script should return with appropriate exit status.
Upvotes: 0
Views: 447
Reputation: 8518
I tried to understand the way you invoke your child script ( Import ) into the parent script ( temp.sh ). Well let me show you what is happening
Import Script
SCHEMA=$1
DBNAME=$2
LOGPATH=/app/dbimport/PreImport_`date +%d%b%Y`.log
export ORACLE_HOME=/oracle/product/11.2.0/db
set -x
for line in `cat "$SCHEMA" | egrep -w 'PANV|PANVPXE'`
do
USER=`echo "$line" |cut -d ';' -f1`
echo "Fetching User : $USER" >> "$LOGPATH"
PASSWORD=`echo "$line" | cut -d ';' -f2`
echo "Fetching Password: $PASSWORD" >> "$LOGPATH"
SOURCE=`echo "$line" | cut -d ';' -f3`
echo "Fetching Source Schema : $SOURCE" >> "$LOGPATH"
done
exit $?
This script will exit with something different than 0 when a problem with the grep occurs, so if the pattern you are looking for it is not there, it will fail.
$ echo "hello" | egrep -i "bye"
$ echo $?
1
Then you are running this script from other program
Launcher
RC=`/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA`
echo "Return code = $RC"
Here is where you have the problem. You are calling the script like it was a function and expecting a result. The variable RC is getting whatever output your script is sending to the STDOUT , nothing else. RC will be always empty, because your script does not send anything to the STDOUT. So, you must understand the difference between getting the result from the child and evaluating what return code produced the child program.
Let me show you an example of what I just explained to you using my own scripts. I have two scripts: the child.sh is just a sqlplus to Oracle. the parent invokes the child the same way you do.
$ more child.sh
#/bin/bash
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" << eof
whenever sqlerror exit failure;
select * from dual ;
eof
if [[ $? -eq 0 ]];
then
exit 0;
else
exit 99;
fi
$ more parent.sh
#!/bin/bash
# run child
var=`/orabatch/ftpcpl/log/child.sh`
echo $var
$ ./child.sh
D
-
X
$ echo $?
0
$ ./parent.sh
D - X
$ echo $?
0
As you see, my parent is getting whatever the child script is sending to the STDOUT. Now let's force an error in the child script to verify that my parent script is still exiting as ok:
$ ./child.sh
select * from dual0
*
ERROR at line 1:
ORA-00942: table or view does not exist
$ ./parent.sh
ERROR at line 1:
ORA-00942: table or view does not exist
$ echo $?
0
As you can see, the output of my operation is the error in the first, however not as an error, but as an output. my parent has ended ok, even you can see that there was an error.
I would rewrite the script as follows:
#/bin/bash
my_path=/app/arjun/scripts
$my_path/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
result=$?
if [ ${result} -ne 0 ];
then
echo "error"
exit 2;
fi
Upvotes: 2
Reputation: 9845
To get the exit code of your script Import.sh
instead of its output, change the script temp.sh
to
/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
RC=$?
echo "Return code = $RC"
or simply
/app/arjun/scripts/Import.sh schema_remap_AANV02_UAT2.txt ARJSCHEMA
echo "Return code = $?"
See the comments for hints how to fix/improve your scripts.
Upvotes: 4