-1

I am trying to do a simple script which runs a few commands N number of times, determined by the user's input. However, when it comes to run the commands (in a for loop) - The variable is ignored:

read -p "Please enter the number of times you wish to fail over: " num

...

  for run in {1..$num}
   do echo "STOP: "
   date
   systemctl stop $broker
   date
   sleep $st
   echo "START: "
   date
   systemctl start $broker
   date
   sleep $st
  done
fi
+ '[' y == n ']'
+ '[' y == N ']'
+ for run in '{1..$num}'
+ echo 'STOP: '
STOP:
+ date
Tue Dec  4 16:14:11 GMT 2018

Can anyone explain why this is happening and what I need to do to rectify? Or does anyone have a better method for this?

Matthew Perrott
  • 257
  • 1
  • 4
  • 8

1 Answers1

1

Just a collection of - more or less secure - and possibly well known approaches:

a=5
eval "for i in {1..$a}; do echo \$i; done"
cat <<< "for i in {1..$a}; do echo \$i; done" | bash
echo "for i in {1..$a}; do echo \$i; done" | bash
for i in $(seq 1 $a); do echo $i; done
RudiC
  • 8,969