Why does this for loop ignore my variable? [duplicate]











up vote
-1
down vote

favorite
1













This question already has an answer here:




  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers




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?










share|improve this question













marked as duplicate by Jeff Schaller, Rui F Ribeiro, Christopher, RalfFriedl, G-Man Dec 4 at 21:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    (short version: brace expansion happens before variable/parameter expansion)
    – Jeff Schaller
    Dec 4 at 16:21















up vote
-1
down vote

favorite
1













This question already has an answer here:




  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers




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?










share|improve this question













marked as duplicate by Jeff Schaller, Rui F Ribeiro, Christopher, RalfFriedl, G-Man Dec 4 at 21:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    (short version: brace expansion happens before variable/parameter expansion)
    – Jeff Schaller
    Dec 4 at 16:21













up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1






This question already has an answer here:




  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers




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?










share|improve this question














This question already has an answer here:




  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers




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?





This question already has an answer here:




  • How can I use $variable in a shell brace expansion of a sequence?

    3 answers








bash scripting variable for






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 4 at 16:15









Matthew Perrott

82




82




marked as duplicate by Jeff Schaller, Rui F Ribeiro, Christopher, RalfFriedl, G-Man Dec 4 at 21:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, Rui F Ribeiro, Christopher, RalfFriedl, G-Man Dec 4 at 21:20


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    (short version: brace expansion happens before variable/parameter expansion)
    – Jeff Schaller
    Dec 4 at 16:21














  • 2




    (short version: brace expansion happens before variable/parameter expansion)
    – Jeff Schaller
    Dec 4 at 16:21








2




2




(short version: brace expansion happens before variable/parameter expansion)
– Jeff Schaller
Dec 4 at 16:21




(short version: brace expansion happens before variable/parameter expansion)
– Jeff Schaller
Dec 4 at 16:21










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    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





    share|improve this answer

























      up vote
      1
      down vote



      accepted










      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





      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 4 at 18:51









        RudiC

        3,7171312




        3,7171312















            Popular posts from this blog

            サソリ

            広島県道265号伴広島線

            Setup Asymptote in Texstudio