[英]Bash for loop in batch on list
I am trying to bash script where it executes following java jar command in batch mode on the basis of size of list.我正在尝试 bash 脚本,它根据列表的大小以批处理模式执行以下 java jar 命令。
i have a list of string with 54 states of USA.我有一个包含美国 54 个州的字符串列表。
list = [USA+CA,USA+TX,USA+TN...]
i need to execute following command in parallel with 4 instances having 4 values as input from list.我需要与具有 4 个值的 4 个实例并行执行以下命令作为列表的输入。
java -jar test-execute.jar --localities=USA+TX,USA+CA,USA+TN,USA+AB
wait till execution is complete for any instance, then start new instance of jar with next 4 states.等到任何实例的执行完成,然后启动具有接下来 4 个状态的 jar 的新实例。
array=( USA+TX,USA+CA,USA+TN,USA+AB )
for i in "${array[@]}"
do
java -jar test-execute.jar --localities= ???
done
I am not able to understand how can i dynamically provide inputs from array into jar execution.我无法理解如何动态地将数组中的输入提供给 jar 执行。
so,所以,
i have list of size 54,我有 54 码的清单,
i need to run 4 java instances in parallel with each instance having 4 unique state as input from list of 54. , once these instances complete, then start next 4 instances with next unique 4 states per instance.我需要并行运行 4 个 Java 实例,每个实例具有 4 个唯一状态作为来自 54 个列表的输入。,一旦这些实例完成,然后开始下一个 4 个实例,每个实例有下一个唯一的 4 个状态。
update:更新:
i have list of 54 states, 16 core machine.我有 54 个州的列表,16 个核心机器。 each java jar instance will use 4 cores, so i can run 4 java instances at a time to to use 16 core machine.
每个 java jar 实例将使用 4 个内核,因此我可以一次运行 4 个 java 实例以使用 16 个内核的机器。
16 core machine
java instance-1 4 states
java instance-2 4 states
java instance-3 4 states
java instance-4 4 states
wait till any of these instances complete, once completed, start new instance with next 4 states until all 54 states has been executed.
please help.请帮忙。
First of all, bash array
must be assigned as a space separated list as:首先,必须将
bash array
分配为以空格分隔的列表,如下所示:
array=("USA+AL" "USA+AK" "USA+AZ" "USA+AR" "USA+AS" "USA+CA" ...)
Then would you please try something like:那么请你尝试这样的事情:
array=("USA+AL" "USA+AK" "USA+AZ" "USA+AR" "USA+AS" "USA+CA" ...)
for (( i = 0; i < ${#array[@]}; i+=4 )); do
echo java -jar test-execute.jar --localities="$(IFS=,; echo "${array[*]:i:4}")"
done
for
loop has a C-like syntax to increment the index by value 4. for
循环具有类似 C 的语法,可将索引递增 4。${array[*]:i:4}
divides the array into sub-arrays of every four elements starting with i'th index.${array[*]:i:4}
将数组分成每四个元素的子数组,从第 i 个索引开始。 The last chunk with two elements are treated as well.$(IFS=,; echo "${array[*]:i:4}")
joins the array with commas to be fed to java
as an argument. $(IFS=,; echo "${array[*]:i:4}")
用逗号连接数组,作为参数提供给java
。 If the output looks good, drop echo
in front of java
.如果输出看起来不错,请将
echo
放在java
前面。
[Edit] [编辑]
As for the parallelism, we can make use of -P
option to xargs
.至于并行性,我们可以使用
xargs
的-P
选项。 Would you please try:你能试试吗:
array=("USA+AL" "USA+AK" "USA+AZ" "USA+AR" "USA+AS" "USA+CA" ...)
for (( i = 0; i < ${#array[@]}; i+=4 )); do
printf "%s\n" "$(IFS=,; echo "${array[*]:i:4}")"
done | xargs -P4 -L1 -I{} java -jar test-execute.jar --localities="{}"
-P4
option generates four processes at a time. -P4
选项一次生成四个进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.