linux – 如何在scp文件的同时并行化for循环?

前端之家收集整理的这篇文章主要介绍了linux – 如何在scp文件的同时并行化for循环?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从machineA运行我的下面的 shell脚本,它将文件machineB和machineC复制到machineA.如果文件不在machineB中,那么它应该在machineC中.

下面的shell脚本会将文件复制到machineA中的TEST1和TEST2目录中.

  1. #!/bin/bash
  2. set -e
  3.  
  4. readonly TEST1=/data01/test1
  5. readonly TEST2=/data02/test2
  6. readonly SERVER_LOCATION=(machineB machineC)
  7. readonly FILE_LOCATION=/data/snapshot
  8.  
  9. 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)
  10. 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)
  11.  
  12. echo $dir1
  13. echo $dir2
  14.  
  15. if [ "$dir1" = "$dir2" ]
  16. then
  17. rm -rf $TEST1/*
  18. rm -rf $TEST2/*
  19. for el in $test1_partition
  20. do
  21. 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/.
  22. done
  23. for sl in $test2_partition
  24. do
  25. 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/.
  26. done
  27. 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

解决方法

更换
  1. done

  1. done &

这将在后台启动两个for循环.

并完成脚本:

  1. wait

这将等待两个for循环完成.

猜你在找的Linux相关文章