Question

I am running my shell script on machineA which copies the files from machineB and machineC to machineA.

If the file is not there in machineB, then it should be there in machineC for sure. So I will try to copy from machineB first, if it is not there in machineB then I will go to machineC to copy the same files. Below is my shell script by which I am copying the files and PRIMARY_PARTITION is an array which has all the file numbers -

for el in "${PRIMARY_PARTITION[@]}"
do
    scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineB:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineC:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/.
done

Now the question is - There might be some cases, where the files are not there in both the machines - machineB and machineC then I would like to exit out of the shell script with non zero status and proper message with which file was missing -

So I did like this, I added another pipe statement at the end but somehow that message is not getting printed out on the screen. And I am pretty much sure those files which is trying to copy are not there in those folders in those two machines -

scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineB:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineC:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/. || echo "File number $el missing on both the machines for primary partition." >&2; exit 1

So it should go to last pipe statement and print it out on console. Right?

Is there anything wrong I did in my last pipe statement?

Était-ce utile?

La solution

As per your solution it should work

scp returns 0 only when it succeeds. so you can write like this:

scp -q machineB:/path/to/your/file . || scp -q machineC:/path/to/your/file . || echo "Sorry no file found"

Now let first you check retrun code of each command like as follows

scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineB:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/.

if [[ $? -ne 0 ]]; then
      echo "No file found"
      exit 1
else
     echo "oh..file is here!"
fi

Same for second command. Now in both case if you get non zero value then your solution must work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top