我正在从machineA运行我的下面的shell脚本,它将文件machineB和machineC复制到machineA 。 如果这些文件不在machineB ,那么它应该在machineC 。
下面的shell脚本将把这些文件复制到machineA TEST1和TEST2目录下。
#!/bin/bash set -e readonly TEST1=/data01/test1 readonly TEST2=/data02/test2 readonly SERVER_LOCATION=(machineB machineC) readonly FILE_LOCATION=/data/snapshot dir1=$(ssh -o "StrictHostKeyChecking no" david@${SERVER_LOCATION[0]} ls -dt1 "$FILE_LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | head -n1) dir2=$(ssh -o "StrictHostKeyChecking no" david@${SERVER_LOCATION[1]} ls -dt1 "$FILE_LOCATION"/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | head -n1) echo $dir1 echo $dir2 if [ "$dir1" = "$dir2" ] then rm -rf $TEST1/* rm -rf $TEST2/* for el in $test1_partition do scp david@${SERVER_LOCATION[0]}:$dir1/pp_monthly_9800_"$el"_200003_5.data $TEST1/. || scp david@${SERVER_LOCATION[1]}:$dir2/pp_monthly_9800_"$el"_200003_5.data $TEST1/. done for sl in $test2_partition do scp david@${SERVER_LOCATION[0]}:$dir1/pp_monthly_9800_"$sl"_200003_5.data $TEST2/. || scp david@${SERVER_LOCATION[1]}:$dir2/pp_monthly_9800_"$sl"_200003_5.data $TEST2/. done fi
https://unix.stackexchange.com/questions/53390/is-there-a-way-to-run-process-parallelly-in-the-loop-of-a-bash-script
目前它首先将machineB和machineC的文件复制到machineA TEST1目录中,如果完成,那么只有它会将machineB和machineC的文件复制到machineA TEST2目录中。是否有任何方法将文件传输到TEST1和TEST2目录同时?
我正在运行Ubuntu 12.04
更换
done
同
done &
这将在后台启动两个for-loop。
并完成脚本:
wait
这将等待两个for循环完成。
尝试这样的事情。
PROCESSES=4 # Number of threads you want for item in ${LIST}; do ACTION ${item} & while (( $(jobs |wc -l) >= (( ${PROCESSES} + 1 )) )); do sleep 0.1 done done wait
这将通过LIST中的项目工作,并在后台对项目执行ACTION。
然后它会检查有多less个作业仍在运行,并等待直到您没有线程化才能继续。