shell script crashes during if statements? [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
for hi in `seq 0 100`
do
new_val=1
if `expr $hi % 5` -eq 5
then
echo hello
elif `expr $hi % 5` -eq 6
then
echo bye
elif `expr $hi % 5` -eq 7
then
echo whats up
fi
echo $new_val
done
Why does this crash? The goal is to check if the loop number module 5 equals 5,6, or 7.
shell
closed as unclear what you're asking by muru, G-Man, cas, Jeff Schaller, ilkkachu Feb 1 at 13:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
 |Â
show 1 more comment
up vote
-1
down vote
favorite
for hi in `seq 0 100`
do
new_val=1
if `expr $hi % 5` -eq 5
then
echo hello
elif `expr $hi % 5` -eq 6
then
echo bye
elif `expr $hi % 5` -eq 7
then
echo whats up
fi
echo $new_val
done
Why does this crash? The goal is to check if the loop number module 5 equals 5,6, or 7.
shell
closed as unclear what you're asking by muru, G-Man, cas, Jeff Schaller, ilkkachu Feb 1 at 13:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Doesn't crash here with bash. Are you using sh?
â Isaac
Feb 1 at 3:26
yes sh is what im using
â Tinler
Feb 1 at 3:27
1
I shall assume that what you meant withif `expr $hi % 5` -eq 5
is actuallyif [ $(expr $hi % 5) -eq 5 ]
(note the [ ] added). You have three elements with this problem.
â Isaac
Feb 1 at 3:29
2
None of the if will be true if the math operation is a modulus because the modulus of 5 can never be 5, 6 or 7. What are you trying to do?
â Isaac
Feb 1 at 3:32
1
Possible duplicate of How do I compare numbers in bash?
â muru
Feb 1 at 3:33
 |Â
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
for hi in `seq 0 100`
do
new_val=1
if `expr $hi % 5` -eq 5
then
echo hello
elif `expr $hi % 5` -eq 6
then
echo bye
elif `expr $hi % 5` -eq 7
then
echo whats up
fi
echo $new_val
done
Why does this crash? The goal is to check if the loop number module 5 equals 5,6, or 7.
shell
for hi in `seq 0 100`
do
new_val=1
if `expr $hi % 5` -eq 5
then
echo hello
elif `expr $hi % 5` -eq 6
then
echo bye
elif `expr $hi % 5` -eq 7
then
echo whats up
fi
echo $new_val
done
Why does this crash? The goal is to check if the loop number module 5 equals 5,6, or 7.
shell
asked Feb 1 at 3:17
Tinler
1295
1295
closed as unclear what you're asking by muru, G-Man, cas, Jeff Schaller, ilkkachu Feb 1 at 13:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by muru, G-Man, cas, Jeff Schaller, ilkkachu Feb 1 at 13:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Doesn't crash here with bash. Are you using sh?
â Isaac
Feb 1 at 3:26
yes sh is what im using
â Tinler
Feb 1 at 3:27
1
I shall assume that what you meant withif `expr $hi % 5` -eq 5
is actuallyif [ $(expr $hi % 5) -eq 5 ]
(note the [ ] added). You have three elements with this problem.
â Isaac
Feb 1 at 3:29
2
None of the if will be true if the math operation is a modulus because the modulus of 5 can never be 5, 6 or 7. What are you trying to do?
â Isaac
Feb 1 at 3:32
1
Possible duplicate of How do I compare numbers in bash?
â muru
Feb 1 at 3:33
 |Â
show 1 more comment
Doesn't crash here with bash. Are you using sh?
â Isaac
Feb 1 at 3:26
yes sh is what im using
â Tinler
Feb 1 at 3:27
1
I shall assume that what you meant withif `expr $hi % 5` -eq 5
is actuallyif [ $(expr $hi % 5) -eq 5 ]
(note the [ ] added). You have three elements with this problem.
â Isaac
Feb 1 at 3:29
2
None of the if will be true if the math operation is a modulus because the modulus of 5 can never be 5, 6 or 7. What are you trying to do?
â Isaac
Feb 1 at 3:32
1
Possible duplicate of How do I compare numbers in bash?
â muru
Feb 1 at 3:33
Doesn't crash here with bash. Are you using sh?
â Isaac
Feb 1 at 3:26
Doesn't crash here with bash. Are you using sh?
â Isaac
Feb 1 at 3:26
yes sh is what im using
â Tinler
Feb 1 at 3:27
yes sh is what im using
â Tinler
Feb 1 at 3:27
1
1
I shall assume that what you meant with
if `expr $hi % 5` -eq 5
is actually if [ $(expr $hi % 5) -eq 5 ]
(note the [ ] added). You have three elements with this problem.â Isaac
Feb 1 at 3:29
I shall assume that what you meant with
if `expr $hi % 5` -eq 5
is actually if [ $(expr $hi % 5) -eq 5 ]
(note the [ ] added). You have three elements with this problem.â Isaac
Feb 1 at 3:29
2
2
None of the if will be true if the math operation is a modulus because the modulus of 5 can never be 5, 6 or 7. What are you trying to do?
â Isaac
Feb 1 at 3:32
None of the if will be true if the math operation is a modulus because the modulus of 5 can never be 5, 6 or 7. What are you trying to do?
â Isaac
Feb 1 at 3:32
1
1
Possible duplicate of How do I compare numbers in bash?
â muru
Feb 1 at 3:33
Possible duplicate of How do I compare numbers in bash?
â muru
Feb 1 at 3:33
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The script corrected shall be something like:
#!/bin/sh
for hi in $(seq 0 100)
do
if [ "$(expr $hi % 5)" -eq 5 ]
then
echo hello
elif [ "$(expr $hi % 5)" -eq 6 ]
then
echo bye
elif [ "$(expr $hi % 5)" -eq 7 ]
then
echo whats up
fi
echo "$hi"
done
But that loop will never enter any of the ifs as the remainder of a modulus 5 operation will never be 5, 6 or 7.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The script corrected shall be something like:
#!/bin/sh
for hi in $(seq 0 100)
do
if [ "$(expr $hi % 5)" -eq 5 ]
then
echo hello
elif [ "$(expr $hi % 5)" -eq 6 ]
then
echo bye
elif [ "$(expr $hi % 5)" -eq 7 ]
then
echo whats up
fi
echo "$hi"
done
But that loop will never enter any of the ifs as the remainder of a modulus 5 operation will never be 5, 6 or 7.
add a comment |Â
up vote
2
down vote
accepted
The script corrected shall be something like:
#!/bin/sh
for hi in $(seq 0 100)
do
if [ "$(expr $hi % 5)" -eq 5 ]
then
echo hello
elif [ "$(expr $hi % 5)" -eq 6 ]
then
echo bye
elif [ "$(expr $hi % 5)" -eq 7 ]
then
echo whats up
fi
echo "$hi"
done
But that loop will never enter any of the ifs as the remainder of a modulus 5 operation will never be 5, 6 or 7.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The script corrected shall be something like:
#!/bin/sh
for hi in $(seq 0 100)
do
if [ "$(expr $hi % 5)" -eq 5 ]
then
echo hello
elif [ "$(expr $hi % 5)" -eq 6 ]
then
echo bye
elif [ "$(expr $hi % 5)" -eq 7 ]
then
echo whats up
fi
echo "$hi"
done
But that loop will never enter any of the ifs as the remainder of a modulus 5 operation will never be 5, 6 or 7.
The script corrected shall be something like:
#!/bin/sh
for hi in $(seq 0 100)
do
if [ "$(expr $hi % 5)" -eq 5 ]
then
echo hello
elif [ "$(expr $hi % 5)" -eq 6 ]
then
echo bye
elif [ "$(expr $hi % 5)" -eq 7 ]
then
echo whats up
fi
echo "$hi"
done
But that loop will never enter any of the ifs as the remainder of a modulus 5 operation will never be 5, 6 or 7.
edited Feb 1 at 10:34
Jeff Schaller
31.4k846105
31.4k846105
answered Feb 1 at 3:37
Isaac
6,6371734
6,6371734
add a comment |Â
add a comment |Â
Doesn't crash here with bash. Are you using sh?
â Isaac
Feb 1 at 3:26
yes sh is what im using
â Tinler
Feb 1 at 3:27
1
I shall assume that what you meant with
if `expr $hi % 5` -eq 5
is actuallyif [ $(expr $hi % 5) -eq 5 ]
(note the [ ] added). You have three elements with this problem.â Isaac
Feb 1 at 3:29
2
None of the if will be true if the math operation is a modulus because the modulus of 5 can never be 5, 6 or 7. What are you trying to do?
â Isaac
Feb 1 at 3:32
1
Possible duplicate of How do I compare numbers in bash?
â muru
Feb 1 at 3:33