3

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
  • 383
  • 14
  • 49
  • 115

2 Answers2

2
#!/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
  • 103,964
  • 12
  • 124
  • 155
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
  • 73,512
  • 8
  • 94
  • 138