I would use parset
:
a() {
filename="$1"
echo "*** Here is ***"
sleep 0.$RANDOM
echo "the response code to"
sleep 0.$RANDOM
echo "$filename"
}
export -f a
# Put the results into a bash array. The order will be kept.
# Here we use "File a" and "File b" as input. Change those to your needs
parset res a ::: "File a" "File b"
echo "${res[0]}"
echo "${res[1]}"
Alternatively:
# If you want the output into an assoc array, convert the results:
# First build an array of the input
input=("File a" "File b")
# Then run the jobs in parallel
parset res a ::: "${input[@]}"
# Finally zip the two arrays to a single associative array
declare -A myassoc
for ((i=0; $i<${#input[@]}; i++)); do
myassoc[${input[i]}]=${res[i]}
done
echo "${myassoc["File a"]}"
parset
is part of GNU Parallel.
To activate parset
you need to activate it as a function. The function is defined as part of env_parallel
.
Do the below and restart your shell.
bash: Put this in $HOME/.bashrc: . `which env_parallel.bash`
E.g. by doing: echo '. `which env_parallel.bash`' >> $HOME/.bashrc
Supports: aliases, functions, variables, arrays
zsh: Put this in $HOME/.zshrc: . `which env_parallel.zsh`
E.g. by doing: echo '. `which env_parallel.zsh`' >> $HOME/.zshenv
Supports: functions, variables, arrays
fish: Unsupported
ksh: Put this in $HOME/.kshrc: source `which env_parallel.ksh`
E.g. by doing: echo 'source `which env_parallel.ksh`' >> $HOME/.kshrc
Supports: aliases, functions, variables, arrays
mksh: Put this in $HOME/.mkshrc: source `which env_parallel.mksh`
E.g. by doing: echo 'source `which env_parallel.mksh`' >> $HOME/.mkshrc
Supports: aliases, functions, variables, arrays
pdksh: Put this in $HOME/.profile: source `which env_parallel.pdksh`
E.g. by doing: echo '. `which env_parallel.pdksh`' >> $HOME/.profile
Supports: aliases, functions, variables, arrays
ash: Put this in $HOME/.profile: . `which env_parallel.ash`
E.g. by doing: echo '. `which env_parallel.ash`' >> $HOME/.profile
Supports: aliases, variables
dash: Put this in $HOME/.profile: . `which env_parallel.dash`
E.g. by doing: echo '. `which env_parallel.dash`' >> $HOME/.profile
Supports: aliases, variables
csh: Unsupported
tcsh: Unsupported
To install in all shells run:
parset --install