0

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.

9
  • @oguzismail have provided whatever is needed . can you tell me what else you need. ? Commented Jul 17, 2020 at 10:26
  • 1
    @oguzismail that i have corrected , provided the contents of schema_remap_AANV02_UAT2.txt file. Import.sh script only uses this txt file. Commented Jul 17, 2020 at 10:35
  • 1
    By the way, see DontReadLinesWithFor and BashFAQ #1 for a way more efficient way to write your loop. Commented Jul 17, 2020 at 11:06
  • 1
    And don't use all-uppercase names for your own variables -- they're used for variables that are meaningful to the system that you shouldn't overwrite. To avoid breaking other programs that expect those built-in variables like $USER to have their original, meaningful values, use lower-case $user when you define it yourself. This is also what the POSIX standard recommends: See pubs.opengroup.org/onlinepubs/9699919799/basedefs/…, keeping in mind when you read it that setting a shell variable overwrites any preexisting like-named environment variable. Commented Jul 17, 2020 at 11:09
  • 1
    @alecxs, ... well, always when the echo succeeds :) -- but right now, they're not even trying to exit $RC, so using RC=$(...command...) instead of ...command...; rc=$? is the place where the existing code is actively wrong. Commented Jul 17, 2020 at 11:15

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.