Shorten if statement check for matching argument

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have this bit of code which does what it's supposed to do:



first_arg="$1";
if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
exit 1;
fi


it's checking ensure that the first argument is one of (patch, major, minor, prerelease).



However, is there a shorter / less-verbose way of doing this?







share|improve this question























    up vote
    0
    down vote

    favorite












    I have this bit of code which does what it's supposed to do:



    first_arg="$1";
    if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
    echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
    exit 1;
    fi


    it's checking ensure that the first argument is one of (patch, major, minor, prerelease).



    However, is there a shorter / less-verbose way of doing this?







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have this bit of code which does what it's supposed to do:



      first_arg="$1";
      if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
      echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
      exit 1;
      fi


      it's checking ensure that the first argument is one of (patch, major, minor, prerelease).



      However, is there a shorter / less-verbose way of doing this?







      share|improve this question











      I have this bit of code which does what it's supposed to do:



      first_arg="$1";
      if [ "$first_arg" != "patch" -a "$first_arg" != "major" -a "$first_arg" != "minor" -a "$first_arg" != "prerelease" ]; then
      echo "First argument needs to match a valid npm version argument (patch, minor, major, etc).";
      exit 1;
      fi


      it's checking ensure that the first argument is one of (patch, major, minor, prerelease).



      However, is there a shorter / less-verbose way of doing this?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 14 at 6:24









      Alexander Mills

      1,873929




      1,873929




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You can use case, which works in any standard shell:



          case "$1" in
          patch|major|minor|prerelease) ;;
          *) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
          esac


          An alternative in Bash/ksh/zsh is to use a regular expression match:



          if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
          echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
          exit 1;
          fi





          share|improve this answer























          • that is the most bizarre syntax ever lol
            – Alexander Mills
            Jun 14 at 6:48










          • @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
            – user1934428
            Jun 15 at 7:17











          • @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
            – user1934428
            Jun 15 at 11:38










          • I assume using quotes like "$1" =~ ^regex$ won't hurt though.
            – Alexander Mills
            Jun 16 at 22:52










          • @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
            – user1934428
            Jun 18 at 6:59










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f449734%2fshorten-if-statement-check-for-matching-argument%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          You can use case, which works in any standard shell:



          case "$1" in
          patch|major|minor|prerelease) ;;
          *) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
          esac


          An alternative in Bash/ksh/zsh is to use a regular expression match:



          if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
          echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
          exit 1;
          fi





          share|improve this answer























          • that is the most bizarre syntax ever lol
            – Alexander Mills
            Jun 14 at 6:48










          • @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
            – user1934428
            Jun 15 at 7:17











          • @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
            – user1934428
            Jun 15 at 11:38










          • I assume using quotes like "$1" =~ ^regex$ won't hurt though.
            – Alexander Mills
            Jun 16 at 22:52










          • @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
            – user1934428
            Jun 18 at 6:59














          up vote
          6
          down vote



          accepted










          You can use case, which works in any standard shell:



          case "$1" in
          patch|major|minor|prerelease) ;;
          *) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
          esac


          An alternative in Bash/ksh/zsh is to use a regular expression match:



          if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
          echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
          exit 1;
          fi





          share|improve this answer























          • that is the most bizarre syntax ever lol
            – Alexander Mills
            Jun 14 at 6:48










          • @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
            – user1934428
            Jun 15 at 7:17











          • @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
            – user1934428
            Jun 15 at 11:38










          • I assume using quotes like "$1" =~ ^regex$ won't hurt though.
            – Alexander Mills
            Jun 16 at 22:52










          • @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
            – user1934428
            Jun 18 at 6:59












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          You can use case, which works in any standard shell:



          case "$1" in
          patch|major|minor|prerelease) ;;
          *) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
          esac


          An alternative in Bash/ksh/zsh is to use a regular expression match:



          if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
          echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
          exit 1;
          fi





          share|improve this answer















          You can use case, which works in any standard shell:



          case "$1" in
          patch|major|minor|prerelease) ;;
          *) echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2; exit 1;;
          esac


          An alternative in Bash/ksh/zsh is to use a regular expression match:



          if ! [[ $1 =~ ^(patch|major|minor|prerelease)$ ]]; then
          echo "First argument needs to match a valid npm version argument (patch, minor, major, etc)." >&2;
          exit 1;
          fi






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 15 at 11:57


























          answered Jun 14 at 6:34









          Stephen Kitt

          139k22301363




          139k22301363











          • that is the most bizarre syntax ever lol
            – Alexander Mills
            Jun 14 at 6:48










          • @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
            – user1934428
            Jun 15 at 7:17











          • @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
            – user1934428
            Jun 15 at 11:38










          • I assume using quotes like "$1" =~ ^regex$ won't hurt though.
            – Alexander Mills
            Jun 16 at 22:52










          • @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
            – user1934428
            Jun 18 at 6:59
















          • that is the most bizarre syntax ever lol
            – Alexander Mills
            Jun 14 at 6:48










          • @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
            – user1934428
            Jun 15 at 7:17











          • @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
            – user1934428
            Jun 15 at 11:38










          • I assume using quotes like "$1" =~ ^regex$ won't hurt though.
            – Alexander Mills
            Jun 16 at 22:52










          • @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
            – user1934428
            Jun 18 at 6:59















          that is the most bizarre syntax ever lol
          – Alexander Mills
          Jun 14 at 6:48




          that is the most bizarre syntax ever lol
          – Alexander Mills
          Jun 14 at 6:48












          @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
          – user1934428
          Jun 15 at 7:17





          @StephenKitt: Just a side note: You don't need to quote $1 inside the [[ .... ]] construct in bash/zsh/ksh.
          – user1934428
          Jun 15 at 7:17













          @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
          – user1934428
          Jun 15 at 11:38




          @StephenKitt : I can't sensibly edit it, because SE requires that at least 6 characters are changed, if I edit a post. In any case, the issue is not really important, and in particular does not affect the validity of the answer, so we can leave it with this. After all, it's in the comments too.
          – user1934428
          Jun 15 at 11:38












          I assume using quotes like "$1" =~ ^regex$ won't hurt though.
          – Alexander Mills
          Jun 16 at 22:52




          I assume using quotes like "$1" =~ ^regex$ won't hurt though.
          – Alexander Mills
          Jun 16 at 22:52












          @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
          – user1934428
          Jun 18 at 6:59




          @AlexanderMills : No. If you enjoy them, use them ;-) Actually, some people always use quotes, because they don't want to think all the time whether we do have to quote or whether the quotes are optional.
          – user1934428
          Jun 18 at 6:59












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449734%2fshorten-if-statement-check-for-matching-argument%23new-answer', 'question_page');

          );

          Post as a guest













































































          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