Using the not equal operator for string comparison The 2019 Stack Overflow Developer Survey Results Are InMultiple string comparison in a single if statement shell script using OR gateChecking first argument of a script if it is -e or -dBash not equal string comparisonWhy does this comparison return not equal?How to print the values of variables with incremented numbers using a loop in shell script?Conditional execution block with || and parentheses problemexit code of the command **before** last?bash or Ksh quotes and command execRemove all duplicate word from string using shell scriptHow to output a shell command into a variable?while loop to check for user input not in for loopProtection of shell command with string variable

What is the motivation for a law requiring 2 parties to consent for recording a conversation

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Correct punctuation for showing a character's confusion

Keeping a retro style to sci-fi spaceships?

What is preventing me from simply constructing a hash that's lower than the current target?

The phrase "to the numbers born"?

Loose spokes after only a few rides

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Getting crown tickets for Statue of Liberty

How do you keep chess fun when your opponent constantly beats you?

If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?

Short story: man watches girlfriend's spaceship entering a 'black hole' (?) forever

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?

Pokemon Turn Based battle (Python)

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

Why isn't the circumferential light around the M87 black hole's event horizon symmetric?

How do I free up internal storage if I don't have any apps downloaded?

ELI5: Why they say that Israel would have been the fourth country to land a spacecraft on the Moon and why they call it low cost?

How to type this arrow in math mode?

What is this sharp, curved notch on my knife for?

How to support a colleague who finds meetings extremely tiring?



Using the not equal operator for string comparison



The 2019 Stack Overflow Developer Survey Results Are InMultiple string comparison in a single if statement shell script using OR gateChecking first argument of a script if it is -e or -dBash not equal string comparisonWhy does this comparison return not equal?How to print the values of variables with incremented numbers using a loop in shell script?Conditional execution block with || and parentheses problemexit code of the command **before** last?bash or Ksh quotes and command execRemove all duplicate word from string using shell scriptHow to output a shell command into a variable?while loop to check for user input not in for loopProtection of shell command with string variable



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








100















I tried to check if the PHONE_TYPE variable contains one of three valid values.



if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
[ "$PHONE_TYPE" != "CISCO" ]
then
echo "Phone type must be nortel,cisco or nec"
exit
fi


The above code did not work for me, so I tried this instead:



if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
[ "$PHONE_TYPE" == "CISCO" ]
then
: # do nothing
else
echo "Phone type must be nortel,cisco or nec"
exit
fi


Are there cleaner ways for this type of task?










share|improve this question






























    100















    I tried to check if the PHONE_TYPE variable contains one of three valid values.



    if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
    [ "$PHONE_TYPE" != "CISCO" ]
    then
    echo "Phone type must be nortel,cisco or nec"
    exit
    fi


    The above code did not work for me, so I tried this instead:



    if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
    [ "$PHONE_TYPE" == "CISCO" ]
    then
    : # do nothing
    else
    echo "Phone type must be nortel,cisco or nec"
    exit
    fi


    Are there cleaner ways for this type of task?










    share|improve this question


























      100












      100








      100


      18






      I tried to check if the PHONE_TYPE variable contains one of three valid values.



      if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
      [ "$PHONE_TYPE" != "CISCO" ]
      then
      echo "Phone type must be nortel,cisco or nec"
      exit
      fi


      The above code did not work for me, so I tried this instead:



      if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
      [ "$PHONE_TYPE" == "CISCO" ]
      then
      : # do nothing
      else
      echo "Phone type must be nortel,cisco or nec"
      exit
      fi


      Are there cleaner ways for this type of task?










      share|improve this question
















      I tried to check if the PHONE_TYPE variable contains one of three valid values.



      if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
      [ "$PHONE_TYPE" != "CISCO" ]
      then
      echo "Phone type must be nortel,cisco or nec"
      exit
      fi


      The above code did not work for me, so I tried this instead:



      if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
      [ "$PHONE_TYPE" == "CISCO" ]
      then
      : # do nothing
      else
      echo "Phone type must be nortel,cisco or nec"
      exit
      fi


      Are there cleaner ways for this type of task?







      shell-script shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 12 at 20:06









      ilkkachu

      63.4k10104181




      63.4k10104181










      asked Mar 14 '13 at 9:50









      munishmunish

      2,687165282




      2,687165282




















          7 Answers
          7






          active

          oldest

          votes


















          137














          I guess you're looking for:



          if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
          [ "$PHONE_TYPE" != "CISCO" ]


          The rules for these equivalents are called De Morgan's laws and in your case meant:



          not(A || B || C) => not(A) && not(B) && not (C)


          Note the change in the boolean operator or and and.



          Whereas you tried to do:



          not(A || B || C) => not(A) || not(B) || not(C)


          Which obviously doesn't work.






          share|improve this answer
































            22














            A much shorter way would be:



            if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
            echo "Phone type must be nortel, cisco or nec."
            fi





            share|improve this answer

























            • I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

              – Milan Simek
              6 hours ago


















            9














            You should use ANDs, not ORs.



            if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
            then


            or



            if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
            then





            share|improve this answer






























              8














              Good answers, and an invaluable lesson ;) Only want to supplement with a note.



              What type of test one choose to use is highly dependent on code, structure, surroundings etc.



              An alternative could be to use a switch or case statement as in:



              case "$PHONE_TYPE" in
              "NORTEL"|"NEC"|"CISCO")
              echo "OK"
              ;;
              *)
              echo "Phone type must be nortel,cisco or nec"
              ;;
              esac


              As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type instead of $PHONE_TYPE.



              Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo" and you're in a world of hurt.



              It will also make it easier to spot which is what.



              Not a have to but a would strongly consider.




              It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:



              valid_phone_type()

              case "$1" in
              "NORTEL"

              if ! valid_phone_type "$phone_type"; then
              echo "Bye."
              exit 1
              fi





              share|improve this answer
































                1














                Use [[ instead



                if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] || 
                [[ "$PHONE_TYPE" != "CISCO" ]]
                then
                echo "Phone type must be nortel,cisco or nec"
                exit 1
                fi





                share|improve this answer

























                • This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                  – ilkkachu
                  Feb 12 at 20:10


















                1














                To correct an above answer (as I can't comment yet):



                PHONE_TYPE="NORTEL"
                if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
                echo "Phone type accepted."
                else
                echo "Error! Phone type must be NORTEL, CISCO or NEC."
                fi


                Please note that you need at least bash 4 for this use of =~

                It doesn't work in bash 3.



                I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)



                The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.






                share|improve this answer
































                  0














                  Just a variation proposal based on @0x80 solution:



                  # define phone brand list
                  phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place

                  # test if user given phone is contained in the list
                  if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
                  echo "found it !"
                  fi





                  share|improve this answer























                    Your Answer








                    StackExchange.ready(function()
                    var channelOptions =
                    tags: "".split(" "),
                    id: "106"
                    ;
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function()
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled)
                    StackExchange.using("snippets", function()
                    createEditor();
                    );

                    else
                    createEditor();

                    );

                    function createEditor()
                    StackExchange.prepareEditor(
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: false,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: null,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader:
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    ,
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    );



                    );













                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f67898%2fusing-the-not-equal-operator-for-string-comparison%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown

























                    7 Answers
                    7






                    active

                    oldest

                    votes








                    7 Answers
                    7






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    137














                    I guess you're looking for:



                    if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
                    [ "$PHONE_TYPE" != "CISCO" ]


                    The rules for these equivalents are called De Morgan's laws and in your case meant:



                    not(A || B || C) => not(A) && not(B) && not (C)


                    Note the change in the boolean operator or and and.



                    Whereas you tried to do:



                    not(A || B || C) => not(A) || not(B) || not(C)


                    Which obviously doesn't work.






                    share|improve this answer





























                      137














                      I guess you're looking for:



                      if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
                      [ "$PHONE_TYPE" != "CISCO" ]


                      The rules for these equivalents are called De Morgan's laws and in your case meant:



                      not(A || B || C) => not(A) && not(B) && not (C)


                      Note the change in the boolean operator or and and.



                      Whereas you tried to do:



                      not(A || B || C) => not(A) || not(B) || not(C)


                      Which obviously doesn't work.






                      share|improve this answer



























                        137












                        137








                        137







                        I guess you're looking for:



                        if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
                        [ "$PHONE_TYPE" != "CISCO" ]


                        The rules for these equivalents are called De Morgan's laws and in your case meant:



                        not(A || B || C) => not(A) && not(B) && not (C)


                        Note the change in the boolean operator or and and.



                        Whereas you tried to do:



                        not(A || B || C) => not(A) || not(B) || not(C)


                        Which obviously doesn't work.






                        share|improve this answer















                        I guess you're looking for:



                        if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
                        [ "$PHONE_TYPE" != "CISCO" ]


                        The rules for these equivalents are called De Morgan's laws and in your case meant:



                        not(A || B || C) => not(A) && not(B) && not (C)


                        Note the change in the boolean operator or and and.



                        Whereas you tried to do:



                        not(A || B || C) => not(A) || not(B) || not(C)


                        Which obviously doesn't work.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 12 at 20:07









                        ilkkachu

                        63.4k10104181




                        63.4k10104181










                        answered Mar 14 '13 at 9:57









                        Nils WernerNils Werner

                        1,71521113




                        1,71521113























                            22














                            A much shorter way would be:



                            if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
                            echo "Phone type must be nortel, cisco or nec."
                            fi





                            share|improve this answer

























                            • I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

                              – Milan Simek
                              6 hours ago















                            22














                            A much shorter way would be:



                            if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
                            echo "Phone type must be nortel, cisco or nec."
                            fi





                            share|improve this answer

























                            • I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

                              – Milan Simek
                              6 hours ago













                            22












                            22








                            22







                            A much shorter way would be:



                            if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
                            echo "Phone type must be nortel, cisco or nec."
                            fi





                            share|improve this answer















                            A much shorter way would be:



                            if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
                            echo "Phone type must be nortel, cisco or nec."
                            fi






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 14 '13 at 17:14

























                            answered Mar 14 '13 at 15:26









                            0x800x80

                            43327




                            43327












                            • I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

                              – Milan Simek
                              6 hours ago

















                            • I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

                              – Milan Simek
                              6 hours ago
















                            I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

                            – Milan Simek
                            6 hours ago





                            I think that should be if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then

                            – Milan Simek
                            6 hours ago











                            9














                            You should use ANDs, not ORs.



                            if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
                            then


                            or



                            if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
                            then





                            share|improve this answer



























                              9














                              You should use ANDs, not ORs.



                              if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
                              then


                              or



                              if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
                              then





                              share|improve this answer

























                                9












                                9








                                9







                                You should use ANDs, not ORs.



                                if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
                                then


                                or



                                if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
                                then





                                share|improve this answer













                                You should use ANDs, not ORs.



                                if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
                                then


                                or



                                if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
                                then






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 14 '13 at 9:57









                                jlliagrejlliagre

                                47.9k786138




                                47.9k786138





















                                    8














                                    Good answers, and an invaluable lesson ;) Only want to supplement with a note.



                                    What type of test one choose to use is highly dependent on code, structure, surroundings etc.



                                    An alternative could be to use a switch or case statement as in:



                                    case "$PHONE_TYPE" in
                                    "NORTEL"|"NEC"|"CISCO")
                                    echo "OK"
                                    ;;
                                    *)
                                    echo "Phone type must be nortel,cisco or nec"
                                    ;;
                                    esac


                                    As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type instead of $PHONE_TYPE.



                                    Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo" and you're in a world of hurt.



                                    It will also make it easier to spot which is what.



                                    Not a have to but a would strongly consider.




                                    It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:



                                    valid_phone_type()

                                    case "$1" in
                                    "NORTEL"

                                    if ! valid_phone_type "$phone_type"; then
                                    echo "Bye."
                                    exit 1
                                    fi





                                    share|improve this answer





























                                      8














                                      Good answers, and an invaluable lesson ;) Only want to supplement with a note.



                                      What type of test one choose to use is highly dependent on code, structure, surroundings etc.



                                      An alternative could be to use a switch or case statement as in:



                                      case "$PHONE_TYPE" in
                                      "NORTEL"|"NEC"|"CISCO")
                                      echo "OK"
                                      ;;
                                      *)
                                      echo "Phone type must be nortel,cisco or nec"
                                      ;;
                                      esac


                                      As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type instead of $PHONE_TYPE.



                                      Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo" and you're in a world of hurt.



                                      It will also make it easier to spot which is what.



                                      Not a have to but a would strongly consider.




                                      It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:



                                      valid_phone_type()

                                      case "$1" in
                                      "NORTEL"

                                      if ! valid_phone_type "$phone_type"; then
                                      echo "Bye."
                                      exit 1
                                      fi





                                      share|improve this answer



























                                        8












                                        8








                                        8







                                        Good answers, and an invaluable lesson ;) Only want to supplement with a note.



                                        What type of test one choose to use is highly dependent on code, structure, surroundings etc.



                                        An alternative could be to use a switch or case statement as in:



                                        case "$PHONE_TYPE" in
                                        "NORTEL"|"NEC"|"CISCO")
                                        echo "OK"
                                        ;;
                                        *)
                                        echo "Phone type must be nortel,cisco or nec"
                                        ;;
                                        esac


                                        As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type instead of $PHONE_TYPE.



                                        Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo" and you're in a world of hurt.



                                        It will also make it easier to spot which is what.



                                        Not a have to but a would strongly consider.




                                        It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:



                                        valid_phone_type()

                                        case "$1" in
                                        "NORTEL"

                                        if ! valid_phone_type "$phone_type"; then
                                        echo "Bye."
                                        exit 1
                                        fi





                                        share|improve this answer















                                        Good answers, and an invaluable lesson ;) Only want to supplement with a note.



                                        What type of test one choose to use is highly dependent on code, structure, surroundings etc.



                                        An alternative could be to use a switch or case statement as in:



                                        case "$PHONE_TYPE" in
                                        "NORTEL"|"NEC"|"CISCO")
                                        echo "OK"
                                        ;;
                                        *)
                                        echo "Phone type must be nortel,cisco or nec"
                                        ;;
                                        esac


                                        As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type instead of $PHONE_TYPE.



                                        Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo" and you're in a world of hurt.



                                        It will also make it easier to spot which is what.



                                        Not a have to but a would strongly consider.




                                        It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:



                                        valid_phone_type()

                                        case "$1" in
                                        "NORTEL"

                                        if ! valid_phone_type "$phone_type"; then
                                        echo "Bye."
                                        exit 1
                                        fi






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Mar 14 '13 at 11:35

























                                        answered Mar 14 '13 at 11:25









                                        RuniumRunium

                                        18.9k43060




                                        18.9k43060





















                                            1














                                            Use [[ instead



                                            if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] || 
                                            [[ "$PHONE_TYPE" != "CISCO" ]]
                                            then
                                            echo "Phone type must be nortel,cisco or nec"
                                            exit 1
                                            fi





                                            share|improve this answer

























                                            • This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                                              – ilkkachu
                                              Feb 12 at 20:10















                                            1














                                            Use [[ instead



                                            if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] || 
                                            [[ "$PHONE_TYPE" != "CISCO" ]]
                                            then
                                            echo "Phone type must be nortel,cisco or nec"
                                            exit 1
                                            fi





                                            share|improve this answer

























                                            • This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                                              – ilkkachu
                                              Feb 12 at 20:10













                                            1












                                            1








                                            1







                                            Use [[ instead



                                            if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] || 
                                            [[ "$PHONE_TYPE" != "CISCO" ]]
                                            then
                                            echo "Phone type must be nortel,cisco or nec"
                                            exit 1
                                            fi





                                            share|improve this answer















                                            Use [[ instead



                                            if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] || 
                                            [[ "$PHONE_TYPE" != "CISCO" ]]
                                            then
                                            echo "Phone type must be nortel,cisco or nec"
                                            exit 1
                                            fi






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jul 8 '15 at 19:26

























                                            answered Jul 8 '15 at 19:20









                                            SwapnilSwapnil

                                            273




                                            273












                                            • This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                                              – ilkkachu
                                              Feb 12 at 20:10

















                                            • This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                                              – ilkkachu
                                              Feb 12 at 20:10
















                                            This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                                            – ilkkachu
                                            Feb 12 at 20:10





                                            This is, of course, wrong. [[ vs [ doesn't help with the logic being off.

                                            – ilkkachu
                                            Feb 12 at 20:10











                                            1














                                            To correct an above answer (as I can't comment yet):



                                            PHONE_TYPE="NORTEL"
                                            if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
                                            echo "Phone type accepted."
                                            else
                                            echo "Error! Phone type must be NORTEL, CISCO or NEC."
                                            fi


                                            Please note that you need at least bash 4 for this use of =~

                                            It doesn't work in bash 3.



                                            I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)



                                            The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.






                                            share|improve this answer





























                                              1














                                              To correct an above answer (as I can't comment yet):



                                              PHONE_TYPE="NORTEL"
                                              if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
                                              echo "Phone type accepted."
                                              else
                                              echo "Error! Phone type must be NORTEL, CISCO or NEC."
                                              fi


                                              Please note that you need at least bash 4 for this use of =~

                                              It doesn't work in bash 3.



                                              I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)



                                              The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.






                                              share|improve this answer



























                                                1












                                                1








                                                1







                                                To correct an above answer (as I can't comment yet):



                                                PHONE_TYPE="NORTEL"
                                                if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
                                                echo "Phone type accepted."
                                                else
                                                echo "Error! Phone type must be NORTEL, CISCO or NEC."
                                                fi


                                                Please note that you need at least bash 4 for this use of =~

                                                It doesn't work in bash 3.



                                                I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)



                                                The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.






                                                share|improve this answer















                                                To correct an above answer (as I can't comment yet):



                                                PHONE_TYPE="NORTEL"
                                                if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
                                                echo "Phone type accepted."
                                                else
                                                echo "Error! Phone type must be NORTEL, CISCO or NEC."
                                                fi


                                                Please note that you need at least bash 4 for this use of =~

                                                It doesn't work in bash 3.



                                                I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)



                                                The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Apr 13 '18 at 16:35

























                                                answered Apr 13 '18 at 14:49









                                                WillWill

                                                213




                                                213





















                                                    0














                                                    Just a variation proposal based on @0x80 solution:



                                                    # define phone brand list
                                                    phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place

                                                    # test if user given phone is contained in the list
                                                    if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
                                                    echo "found it !"
                                                    fi





                                                    share|improve this answer



























                                                      0














                                                      Just a variation proposal based on @0x80 solution:



                                                      # define phone brand list
                                                      phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place

                                                      # test if user given phone is contained in the list
                                                      if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
                                                      echo "found it !"
                                                      fi





                                                      share|improve this answer

























                                                        0












                                                        0








                                                        0







                                                        Just a variation proposal based on @0x80 solution:



                                                        # define phone brand list
                                                        phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place

                                                        # test if user given phone is contained in the list
                                                        if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
                                                        echo "found it !"
                                                        fi





                                                        share|improve this answer













                                                        Just a variation proposal based on @0x80 solution:



                                                        # define phone brand list
                                                        phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place

                                                        # test if user given phone is contained in the list
                                                        if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
                                                        echo "found it !"
                                                        fi






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered yesterday









                                                        tdagettdaget

                                                        1014




                                                        1014



























                                                            draft saved

                                                            draft discarded
















































                                                            Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid


                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.

                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f67898%2fusing-the-not-equal-operator-for-string-comparison%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            -shell, shell-script

                                                            Popular posts from this blog

                                                            Frič See also Navigation menuinternal link

                                                            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                                                            fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox