Why does this for loop ignore my variable? [duplicate]
up vote
-1
down vote
favorite
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?
bash scripting variable for
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.
add a comment |
up vote
-1
down vote
favorite
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?
bash scripting variable for
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
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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?
bash scripting variable for
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
bash scripting variable for
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Dec 4 at 18:51
RudiC
3,7171312
3,7171312
add a comment |
add a comment |
2
(short version: brace expansion happens before variable/parameter expansion)
– Jeff Schaller
Dec 4 at 16:21