如何运行批处理命令?

分享于 

2分钟阅读

ubuntu

  繁體

问题:

我有以下脚本:


command file1 &


command file2 &


command file3 &


command file4 &


command file5 &


command file6 &


# SOME OTHER COMMAND 


command file7 &


command file8 &


command file9 &


command file10 &


command file11 &


command file12 &




答案1:

使用bash shell的内置wait命令。来自man bash


 wait [-n] [n ...]


 Wait for each specified child process and return its termination


 status. Each n may be a process ID or a job specification; if a


 job spec is given, all processes in that job's pipeline are


 waited for. If n is not given, all currently active child pro‐


 cesses are waited for, and the return status is zero. If the -n


 option is supplied, wait waits for any job to terminate and


 returns its exit status. If n specifies a non-existent process


 or job, the return status is 127. Otherwise, the return status


 is the exit status of the last process or job waited for.

所以


command file1 &


command file2 &


command file3 &


command file4 &


command file5 &


command file6 &



wait



command file7 &


command file8 &


command file9 &


command file10 &


command file11 &


command file12 &




答案2:


batch() {


 for arg; do


 theCommand "$arg" &


 done


 wait


}



batch file{1..6}


batch file{7..12}




答案3

xargs在使用不同的参数运行同一命令时是一个很好的选择。


echo file{1..32} | xargs -P 32 -n 1 command



-P 32告诉xargs一次最多运行32个进程,-n 1表示向命令传递一个参数。


COM  COMM  Comma  bat  BASH  commands