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
ormachineC
, 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 inmachineB
andmachineC
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?