Checking first argument of a script if it is -e or -d [duplicate]












1
















This question already has an answer here:




  • Using the not equal operator for string comparison

    6 answers




I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:



if [ "$1" != "-e" ] || [ "$1" != "-d" ]; then
echo "Error: First argument must be either -e or -d"
exit 1
fi


this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?










share|improve this question















marked as duplicate by Kusalananda, ilkkachu, jimmij, Jeff Schaller, Stephen Harris Feb 13 at 3:11


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





    Just the logic of it :) if it's -d, it's not -e, so your test is backwards.

    – Jeff Schaller
    Feb 12 at 19:56











  • You want "not (-d or -e)", i.e. "not -d and not -e". Voting to close as off-topic (typo).

    – Kusalananda
    Feb 12 at 19:58











  • oh okay, Thank you all ^^

    – Pengibaby
    Feb 12 at 19:59
















1
















This question already has an answer here:




  • Using the not equal operator for string comparison

    6 answers




I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:



if [ "$1" != "-e" ] || [ "$1" != "-d" ]; then
echo "Error: First argument must be either -e or -d"
exit 1
fi


this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?










share|improve this question















marked as duplicate by Kusalananda, ilkkachu, jimmij, Jeff Schaller, Stephen Harris Feb 13 at 3:11


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





    Just the logic of it :) if it's -d, it's not -e, so your test is backwards.

    – Jeff Schaller
    Feb 12 at 19:56











  • You want "not (-d or -e)", i.e. "not -d and not -e". Voting to close as off-topic (typo).

    – Kusalananda
    Feb 12 at 19:58











  • oh okay, Thank you all ^^

    – Pengibaby
    Feb 12 at 19:59














1












1








1









This question already has an answer here:




  • Using the not equal operator for string comparison

    6 answers




I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:



if [ "$1" != "-e" ] || [ "$1" != "-d" ]; then
echo "Error: First argument must be either -e or -d"
exit 1
fi


this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?










share|improve this question

















This question already has an answer here:




  • Using the not equal operator for string comparison

    6 answers




I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:



if [ "$1" != "-e" ] || [ "$1" != "-d" ]; then
echo "Error: First argument must be either -e or -d"
exit 1
fi


this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?





This question already has an answer here:




  • Using the not equal operator for string comparison

    6 answers








shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 20:54









ilkkachu

60.1k997170




60.1k997170










asked Feb 12 at 19:53









PengibabyPengibaby

111




111




marked as duplicate by Kusalananda, ilkkachu, jimmij, Jeff Schaller, Stephen Harris Feb 13 at 3:11


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 Kusalananda, ilkkachu, jimmij, Jeff Schaller, Stephen Harris Feb 13 at 3:11


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





    Just the logic of it :) if it's -d, it's not -e, so your test is backwards.

    – Jeff Schaller
    Feb 12 at 19:56











  • You want "not (-d or -e)", i.e. "not -d and not -e". Voting to close as off-topic (typo).

    – Kusalananda
    Feb 12 at 19:58











  • oh okay, Thank you all ^^

    – Pengibaby
    Feb 12 at 19:59














  • 2





    Just the logic of it :) if it's -d, it's not -e, so your test is backwards.

    – Jeff Schaller
    Feb 12 at 19:56











  • You want "not (-d or -e)", i.e. "not -d and not -e". Voting to close as off-topic (typo).

    – Kusalananda
    Feb 12 at 19:58











  • oh okay, Thank you all ^^

    – Pengibaby
    Feb 12 at 19:59








2




2





Just the logic of it :) if it's -d, it's not -e, so your test is backwards.

– Jeff Schaller
Feb 12 at 19:56





Just the logic of it :) if it's -d, it's not -e, so your test is backwards.

– Jeff Schaller
Feb 12 at 19:56













You want "not (-d or -e)", i.e. "not -d and not -e". Voting to close as off-topic (typo).

– Kusalananda
Feb 12 at 19:58





You want "not (-d or -e)", i.e. "not -d and not -e". Voting to close as off-topic (typo).

– Kusalananda
Feb 12 at 19:58













oh okay, Thank you all ^^

– Pengibaby
Feb 12 at 19:59





oh okay, Thank you all ^^

– Pengibaby
Feb 12 at 19:59










2 Answers
2






active

oldest

votes


















1














If $1 is -d, the first condition is true. If $1 is -e, the second condition is true. If it's anything else, both are true. Since you joined them with || (or), the then branch of the if is taken if either is true. Therefore, the then branch is always taken.



You need to use if [ "$1" != "-e" ] && [ "$1" != "-d" ] to take the then branch only if $1 is not -e and is not -d.






share|improve this answer

































    1














    case $1 in
    -d) ;; # nothing to do
    -e) ;; # nothing to do
    *)
    echo 'Error: First argument must be either -e or -d' >&2
    exit 1
    esac


    This would trigger the exit 1 if $1 is anything other than -d or -e.



    Alternatively, set a flag depending on the option used and the act on it (I tend to separate parsing the command line options from acting on them):



    case $1 in
    -d) dflag=1 ;;
    -e) dflag=0 ;;
    *)
    echo 'Error: First argument must be either -e or -d' >&2
    exit 1
    esac

    # Getting here means:
    # - $1 was either -d or -e
    # - $dflag will show which one it was

    if [ "$dflag" -eq 1 ]; then
    # act on -d option
    else
    # act on -e option
    fi




    As ilkkachu has already elucidated, your issue (apart from the missing double quote) is one of logic (boolean operations). Above, I'm just showing another way of doing the operation. case...esac statements are often used to process command line options.






    share|improve this answer
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If $1 is -d, the first condition is true. If $1 is -e, the second condition is true. If it's anything else, both are true. Since you joined them with || (or), the then branch of the if is taken if either is true. Therefore, the then branch is always taken.



      You need to use if [ "$1" != "-e" ] && [ "$1" != "-d" ] to take the then branch only if $1 is not -e and is not -d.






      share|improve this answer






























        1














        If $1 is -d, the first condition is true. If $1 is -e, the second condition is true. If it's anything else, both are true. Since you joined them with || (or), the then branch of the if is taken if either is true. Therefore, the then branch is always taken.



        You need to use if [ "$1" != "-e" ] && [ "$1" != "-d" ] to take the then branch only if $1 is not -e and is not -d.






        share|improve this answer




























          1












          1








          1







          If $1 is -d, the first condition is true. If $1 is -e, the second condition is true. If it's anything else, both are true. Since you joined them with || (or), the then branch of the if is taken if either is true. Therefore, the then branch is always taken.



          You need to use if [ "$1" != "-e" ] && [ "$1" != "-d" ] to take the then branch only if $1 is not -e and is not -d.






          share|improve this answer















          If $1 is -d, the first condition is true. If $1 is -e, the second condition is true. If it's anything else, both are true. Since you joined them with || (or), the then branch of the if is taken if either is true. Therefore, the then branch is always taken.



          You need to use if [ "$1" != "-e" ] && [ "$1" != "-d" ] to take the then branch only if $1 is not -e and is not -d.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 12 at 20:01

























          answered Feb 12 at 19:58









          ilkkachuilkkachu

          60.1k997170




          60.1k997170

























              1














              case $1 in
              -d) ;; # nothing to do
              -e) ;; # nothing to do
              *)
              echo 'Error: First argument must be either -e or -d' >&2
              exit 1
              esac


              This would trigger the exit 1 if $1 is anything other than -d or -e.



              Alternatively, set a flag depending on the option used and the act on it (I tend to separate parsing the command line options from acting on them):



              case $1 in
              -d) dflag=1 ;;
              -e) dflag=0 ;;
              *)
              echo 'Error: First argument must be either -e or -d' >&2
              exit 1
              esac

              # Getting here means:
              # - $1 was either -d or -e
              # - $dflag will show which one it was

              if [ "$dflag" -eq 1 ]; then
              # act on -d option
              else
              # act on -e option
              fi




              As ilkkachu has already elucidated, your issue (apart from the missing double quote) is one of logic (boolean operations). Above, I'm just showing another way of doing the operation. case...esac statements are often used to process command line options.






              share|improve this answer






























                1














                case $1 in
                -d) ;; # nothing to do
                -e) ;; # nothing to do
                *)
                echo 'Error: First argument must be either -e or -d' >&2
                exit 1
                esac


                This would trigger the exit 1 if $1 is anything other than -d or -e.



                Alternatively, set a flag depending on the option used and the act on it (I tend to separate parsing the command line options from acting on them):



                case $1 in
                -d) dflag=1 ;;
                -e) dflag=0 ;;
                *)
                echo 'Error: First argument must be either -e or -d' >&2
                exit 1
                esac

                # Getting here means:
                # - $1 was either -d or -e
                # - $dflag will show which one it was

                if [ "$dflag" -eq 1 ]; then
                # act on -d option
                else
                # act on -e option
                fi




                As ilkkachu has already elucidated, your issue (apart from the missing double quote) is one of logic (boolean operations). Above, I'm just showing another way of doing the operation. case...esac statements are often used to process command line options.






                share|improve this answer




























                  1












                  1








                  1







                  case $1 in
                  -d) ;; # nothing to do
                  -e) ;; # nothing to do
                  *)
                  echo 'Error: First argument must be either -e or -d' >&2
                  exit 1
                  esac


                  This would trigger the exit 1 if $1 is anything other than -d or -e.



                  Alternatively, set a flag depending on the option used and the act on it (I tend to separate parsing the command line options from acting on them):



                  case $1 in
                  -d) dflag=1 ;;
                  -e) dflag=0 ;;
                  *)
                  echo 'Error: First argument must be either -e or -d' >&2
                  exit 1
                  esac

                  # Getting here means:
                  # - $1 was either -d or -e
                  # - $dflag will show which one it was

                  if [ "$dflag" -eq 1 ]; then
                  # act on -d option
                  else
                  # act on -e option
                  fi




                  As ilkkachu has already elucidated, your issue (apart from the missing double quote) is one of logic (boolean operations). Above, I'm just showing another way of doing the operation. case...esac statements are often used to process command line options.






                  share|improve this answer















                  case $1 in
                  -d) ;; # nothing to do
                  -e) ;; # nothing to do
                  *)
                  echo 'Error: First argument must be either -e or -d' >&2
                  exit 1
                  esac


                  This would trigger the exit 1 if $1 is anything other than -d or -e.



                  Alternatively, set a flag depending on the option used and the act on it (I tend to separate parsing the command line options from acting on them):



                  case $1 in
                  -d) dflag=1 ;;
                  -e) dflag=0 ;;
                  *)
                  echo 'Error: First argument must be either -e or -d' >&2
                  exit 1
                  esac

                  # Getting here means:
                  # - $1 was either -d or -e
                  # - $dflag will show which one it was

                  if [ "$dflag" -eq 1 ]; then
                  # act on -d option
                  else
                  # act on -e option
                  fi




                  As ilkkachu has already elucidated, your issue (apart from the missing double quote) is one of logic (boolean operations). Above, I'm just showing another way of doing the operation. case...esac statements are often used to process command line options.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 12 at 20:16

























                  answered Feb 12 at 20:03









                  KusalanandaKusalananda

                  133k17253416




                  133k17253416















                      Popular posts from this blog

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀