4

I'm trying to write a bash script that loops through two variables:

#!/bin/bash
for i in sd fd dir && j in storage file director
 do
   echo "restarting bacula $j daemon"
   /sbin/service bacula-$i restart
   echo
done

The code above is obviously wrong. But I want i & j to move in lock step with one another. Can someone help me with a way to achieve this?

Thanks

bluethundr
  • 1,005
  • 17
  • 68
  • 141

2 Answers2

4
#!/bin/bash
a=(sd fd dir)
b=(storage file director)
for k in "${!a[@]}"
do
   echo "restarting bacula ${b[k]} daemon"
   /sbin/service "bacula-${a[k]}" restart
   echo
done
John1024
  • 109,961
  • 14
  • 137
  • 171
0

Use arrays and a manual loop.

a=(sd fd dir)
b=(storage file director)

for ((i = 0; i <= ${#a}; i++)); do
    echo "restarting bacula ${b[i]} daemon"
    /sbin/service "bacula-${a[i]}" restart
done
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148