如果远程服务器上不存在文件或磁盘已满,则从 subshell 退出 shell 脚本?

exit shell script from subshell if files are not present on remote server or disk is full?

我正在使用以下脚本从远程服务器并行复制文件:

#!/bin/bash

primary=/bat/primary
secondary=/bat/secondary

do_copy() {
  el=
  primsec=
  (scp -C -o StrictHostKeyChecking=no \
   goldy@"$remote":"$pod1"/abc_187_"$el"_111_8.data "$primsec"/. > /dev/null 2>&1)\
  || \
  (scp -C -o StrictHostKeyChecking=no \
   goldy@"$remote":"$pod2"/abc_187_"$el"_111_8.data "$primsec"/. > /dev/null 2>&1)\
  || \
  (scp -C -o StrictHostKeyChecking=no \
   goldy@"$remote":"$pod3"/abc_187_"$el"_111_8.data "$primsec"/. > /dev/null 2>&1)\
  || \
  (scp -C -o StrictHostKeyChecking=no \
   goldy@"$remote":"$pod4"/abc_187_"$el"_111_8.data "$primsec"/. > /dev/null 2>&1)
}
export -f do_copy

parallel -j "" do_copy {} $primary ::: ${pri[@]} &
parallel -j "" do_copy {} $secondary ::: ${snd[@]} &
wait
echo "all files copied"

在远程服务器上,我有四个文件夹,分别用 $pod1$pod2$pod3$pod4 表示。如果文件不在 $pod1 中,那么它应该在 $pod2 中,或者在 $pod3 中,或者在 $pod4 文件夹中。

现在我要做的是:

我尝试在最后一个 scp 命令旁边添加 exit 命令,但它只是从 subshell 退出,而不是从整个脚本退出。有什么办法可以做到这些吗?

我假设该文件只存在于一个 pod 上,因此尝试所有 pods 不是问题。

#!/bin/bash

primary=/bat/primary
secondary=/bat/secondary

# Activate parset and env_parallel if not already done
. `which env_parallel.bash`

# Make array with $primary as many times as elements in $pri
parset prim -N0 echo $primary ::: ${pri[@]} 
# Make array with $secondary as many times as elements in $pri
parset seco -N0 echo $secondary ::: ${sec[@]}
export remote

do_copy() {
  el=
  primsec=
  pod=
  scp -C -o StrictHostKeyChecking=no \
   goldy@"$remote":"$pod"/abc_187_"$el"_111_8.data "$primsec"/
}
export -f do_copy
copy_one_file() {
  parallel do_copy   ::: pod{1..4}
  if [ $? == 4 ] ; then
    return 1
  else
    return 0
  fi
}
export -f copy_one_file
parallel --halt now,fail=1 copy_one_file :::  ${pri[@]} ${sec[@]} :::+ ${prim[@]} ${seco[@]}

parset$primary 创建一个数组,只要 $pri.

:::+$pri 中的元素链接到 $prim 中的元素。

parset.

需要并行版本 > 20170422