Itérer à travers des sets d'arguments de command dans Bash

J'ai plusieurs "sets" d'arguments pour une command que j'ai besoin d'exécuter en séquence (bien, en parallèle, techniquement). J'ai aussi besoin de répéter la même logique après avoir exécuté chaque command.

#!/bin/bash local pids=() # Run commands in parallel. my_command "$URL_ONE" "$URL_ONE_TEXT" "${TMP_DIR}/some_dir" & pids+=("$!") my_command "$URL_ONE" "$URL_TWO_TEXT" "${TMP_DIR}/some_similar_dir" & pids+=("$!") my_command "$URL_TWO" "$URL_TWO_TEXT" "${TMP_DIR}/third_dir" & pids+=("$!") my_command "$URL_THREE" "$URL_THREE_TEXT" "${TMP_DIR}/fourth_dir" & pids+=("$!") # ... # Wait for parallel commands to complete and exit if any fail. for pid in "${pids[@]}"; do wait "$pid" if [[ $? -ne 0 ]]; then errecho "Failed." exit 1 fi done 

Plutôt que de répéter si pids+=("$!") des pids+=("$!") Et d'autres parties, j'aimerais définir un tableau / un set d'arguments, le parcourir et exécuter la même logique pour chaque set d'arguments. Par exemple:

 #!/bin/bash # This wouldn't actually work... ARG_SETS=( ("$URL_ONE" "$URL_ONE_TEXT" "${TMP_DIR}/some_dir") ("$URL_ONE" "$URL_TWO_TEXT" "${TMP_DIR}/some_similar_dir") ("$URL_TWO" "$URL_TWO_TEXT" "${TMP_DIR}/third_dir") ("$URL_THREE" "$URL_THREE_TEXT" "${TMP_DIR}/fourth_dir") ) for arg1 arg2 arg3 in "$ARG_SETS[@]"; do my_command "$arg1" "$arg2" "$arg3" & pids+=("$!") done 

Mais Bash ne prend pas en charge les arrays multidimensionnels. Quelqu'un at-il des idées pour un bon model pour rendre ce nettoyeur, ou faire quelque chose de similaire dans la design de mon deuxième exemple? Merci!

Cette approche utilise trois arrays, un pour chaque argument de la command my_command :

 pids=() a=("$URL_ONE" "$URL_ONE" "$URL_TWO" "$URL_THREE") b=("$URL_ONE_TEXT" "$URL_TWO_TEXT" "$URL_TWO_TEXT" "$URL_THREE_TEXT") c=("${TMP_DIR}/some_dir" "${TMP_DIR}/some_similar_dir" "${TMP_DIR}/third_dir" "${TMP_DIR}/fourth_dir") for i in ${!a[@]} do my_command "${a[$i]}" "${b[$i]}" "${c[$i]}" & pids+=("$!") done # Wait for parallel commands to complete and exit if any fail. for pid in "${pids[@]}" do if ! wait "$pid" then errecho "Failed." exit 1 fi done 

Style alternatif

En fonction du nombre de commands à exécuter, vous pouvez envisager l'alternative suivante pour définir les arrays:

 a=(); b=(); c=() a+=("$URL_ONE"); b+=("$URL_ONE_TEXT"); c+=("${TMP_DIR}/some_dir") a+=("$URL_ONE"); b+=("$URL_TWO_TEXT"); c+=("${TMP_DIR}/some_similar_dir") a+=("$URL_TWO"); b+=("$URL_TWO_TEXT"); c+=("${TMP_DIR}/third_dir") a+=("$URL_THREE"); b+=("$URL_THREE_TEXT"); c+=("${TMP_DIR}/fourth_dir") 

Je ne vois pas vraiment le besoin de arrays, et irais pour quelque chose de simple comme:

 f(){ my_command "$@" & pids+=("$!") } f "$URL_ONE" "$URL_ONE_TEXT" "${TMP_DIR}/some_dir" f "$URL_ONE" "$URL_TWO_TEXT" "${TMP_DIR}/some_similar_dir" f "$URL_TWO" "$URL_TWO_TEXT" "${TMP_DIR}/third_dir" f "$URL_THREE" "$URL_THREE_TEXT" "${TMP_DIR}/fourth_dir"