简体   繁体   中英

BASH Script Using & in a function

Trying to execute a function with an & so the For Loop will continue to execute without waiting for the job to be done.

My Function

perform_hdparm() {
hdparm -t "$1" | awk '/seconds/{print $11}'}

The For Loop

for drive in ${hddletter[@]}; do
printf '%s: %s\n' "$drive" "$(perform_hdparm "$drive")"

The Array contains

/dev/sda /dev/sdb /dev/sdk

So where do I put the & at to make things continue or can I? Just trying to speed things up.

Thank you

Add it after the command you want to run in the background:

for drive in "${hddletter[@]}"
do
  printf '%s: %s\n' "$drive" "$(perform_hdparm "$drive")" &
done
wait

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM