How to exit out of the shell script with non zero

2019-09-09 09:52发布

问题:

I am running my below shell script from machineA which is copying the files from machineB and machineC into machineA. If the files are not there in machineB, then it should be there in machineC. Files won't be duplicated across machineB and machineC, they will have unique files so let's say I have total 100 files, so it might be possible 40 files are in machineB and remaining 60 files will be in machineC.

I need to copy some files in PRIMARY directory in machineA and some files in SECONDARY directory in machineA.

Below is my shell script which works fine but I would like to handle some corner cases such as -

  • In some cases suppose if the files are not available either in machineB or machineC, then I would like to exit out of the shell script with non zero status (which is unsuccessfull) and return the error with the file number that are not available in machineB and machineC both.

Below is my shell script -

#!/bin/bash

readonly PRIMARY=/test01/primary
readonly SECONDARY=/test02/secondary
readonly FILERS_LOCATION=(machineB machineC)
readonly LOCATION=/bat/test/pe_t1_snapshot

PRIMARY_PARTITION=(1003 1012 1031) # this will have more file numbers
SECONDARY_PARTITION=(1005 1032 1067)  # this will have more file numbers

dir1=/bat/test/pe_t1_snapshot/20140320
dir2=/bat/test/pe_t1_snapshot/20140320

if [ "$dir1" = "$dir2" ]
then

    # delete first and then copy the files in primary directory
    find "$PRIMARY" -mindepth 1 -delete
    for el in "${PRIMARY_PARTITION[@]}"
    do
        scp user@${FILERS_LOCATION[0]}:$dir1/t1_weekly_1680_"$el"_200003_5.data $PRIMARY/. || scp user@${FILERS_LOCATION[1]}:$dir2/t1_weekly_1680_"$el"_200003_5.data $PRIMARY/.
    done

    # delete first and then copy the files in secondary directory   
    find "$SECONDARY" -mindepth 1 -delete
    for sl in "${SECONDARY_PARTITION[@]}"
    do
        scp user@${FILERS_LOCATION[0]}:$dir1/t1_weekly_1680_"$sl"_200003_5.data $SECONDARY/. || scp user@${FILERS_LOCATION[1]}:$dir2/t1_weekly_1680_"$sl"_200003_5.data $SECONDARY/.
    done
fi

As I am calling the above shell script from the Python subprocess module -

proc = subprocess.Popen(shell_script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable='/bin/bash')
(stdout, stderr) = proc.communicate()
if proc.returncode != 0:
    # log an error with message which file numbers are missing in machineB and machine
else:
    # log successfull status

In my above shell script I am using pipe || operator. If the files are not there in machineB then try in machineC using pipe || operator.

Is this possible to do?

回答1:

If you need get the file name and exist code, try this, you need set for secondary scp as well with different exist code, such as exit 102

# delete first and then copy the files in primary directory
    find "$PRIMARY" -mindepth 1 -delete
    for el in "${PRIMARY_PARTITION[@]}"
    do
        if `scp user@${FILERS_LOCATION[0]}:$dir1/t1_weekly_1680_"$el"_200003_5.data $PRIMARY/. || scp user@${FILERS_LOCATION[1]}:$dir2/t1_weekly_1680_"$el"_200003_5.data $PRIMARY/. ` ; then
            :
        else
            echo " scp is not successful on file $dir1/t1_weekly_1680_${el}_200003_5.data"
            exit 101
    done


回答2:

You can use

exit $?

or

exit $LINENO

to get out of shell script with non success code.