linux - Parallel Processing in While Loop -
i have .sh reading in lines .txt used parameters shell script.every 2 lines process in parallel. currently: current code read in lines in file (2 @ time) first .sh call, lines second .sh call, last .sh call
problem: need first 2 lines in first .sh, second .sh, last .sh..then loop , process next 2 lines hheeelllppp!!! :)
now:
cat xxxxx.txt | while read line; export step=${line//\"/} export step=executemodel_${step//,/_} export pov=$line $dir"/hpm_ws_client.sh" processcalcscriptoptions "$appname" "$pov" "$layers" "$stages" "" "$stages" "$stages" false > "$dir""/"$step"_processid.log" $dir_shell_model"/check_process_status.sh" "$dir" "$step" > "$dir""/""$step""_monitor.log" & $dir"/hpm_ws_client.sh" processcalcscriptoptions "$appname" "$pov" "$layers" "" "" "$stage_final" "" true > "$dir""/""$step""_processid.log" $dir"/check_process_status.sh" "$dir" "$step" > "$dir""/""$step""_monitor.log" & $dir"/hpm_ws_client.sh" processgenealogyexecutionpaths "$appname" "$pov" "$layers" "$genpath_01" > "$dir""/""$step""_processid.log" $dir"/check_process_status.sh" "$dir" "$step" > "$dir""/""$step""_monitor.log" & if (( ++i % 2 == 0)) echo "waiting..." wait fi done
i cannot see trying do, hope 1 of these 2 syntaxes - either reading 2 lines @ time, or loading parameters array re-using them.
so, if file.txt
looks this:
line 1 line 2 line 3 line 4 line 5 line 6
example 1 - 2 reads
#!/bin/bash while read && read b; echo $a, $b done < file.txt
output
line 1, line 2 line 3, line 4 line 5, line 6
example 2 - bash array
#!/bin/bash declare -a params while ifs=$'\n' read -r z; params+=("${z}") done < file.txt # print elements out (( i=0;i<${#params[@]};i++ )) echo ${params[$i]} done
output
line 1 line 2 line 3 line 4 line 5 line 6
example 3 - gnu parallel
or, suggested in comment, use gnu parallel
this
parallel -k -l2 echo {1} {2} < file.txt
output
line 1 line 2 line 3 line 4 line 5 line 6
where -k
means "keep output in order" , -l2
means "take 2 lines @ time file.txt".
this has advantage that, if want run 8 scripts @ time in parallel, specify -j 8
parallel
, job done.
Comments
Post a Comment