shell script crashes during if statements? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this 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 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




    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














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.







share|improve this 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 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




    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












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.







share|improve this question












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.









share|improve this question











share|improve this question




share|improve this question










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 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




    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










  • 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 actually if [ $(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










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.






share|improve this answer





























    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.






    share|improve this answer


























      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.






      share|improve this answer
























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 1 at 10:34









        Jeff Schaller

        31.4k846105




        31.4k846105










        answered Feb 1 at 3:37









        Isaac

        6,6371734




        6,6371734












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay