Questions about understanding a template of using bash's getopts











up vote
0
down vote

favorite












A stackoverflow post has a template for handling command line arguments.



Does the test [ $# == 0 ] mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.



In the case statement, how different are the two cases *) and "?") ? They seem the same.



# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi

while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done

shift $(($OPTIND - 1))

param1=$1
param2=$2









share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • There are two questions here.
    – ctrl-alt-delor
    Nov 22 at 12:43















up vote
0
down vote

favorite












A stackoverflow post has a template for handling command line arguments.



Does the test [ $# == 0 ] mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.



In the case statement, how different are the two cases *) and "?") ? They seem the same.



# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi

while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done

shift $(($OPTIND - 1))

param1=$1
param2=$2









share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • There are two questions here.
    – ctrl-alt-delor
    Nov 22 at 12:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











A stackoverflow post has a template for handling command line arguments.



Does the test [ $# == 0 ] mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.



In the case statement, how different are the two cases *) and "?") ? They seem the same.



# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi

while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done

shift $(($OPTIND - 1))

param1=$1
param2=$2









share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











A stackoverflow post has a template for handling command line arguments.



Does the test [ $# == 0 ] mean that a bash script shouldn't be run without any argument? As a template, I think that scripts generally do not necessarily require any argument.



In the case statement, how different are the two cases *) and "?") ? They seem the same.



# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi

while getopts ":i:vh" optname
do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"i")
echo "-i argument: $OPTARG"
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done

shift $(($OPTIND - 1))

param1=$1
param2=$2






bash shell-script getopts






share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 22 at 12:32









Ben

2789




2789




New contributor




Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • There are two questions here.
    – ctrl-alt-delor
    Nov 22 at 12:43


















  • There are two questions here.
    – ctrl-alt-delor
    Nov 22 at 12:43
















There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43




There are two questions here.
– ctrl-alt-delor
Nov 22 at 12:43










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2 as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i.



"?", vs *



Yes they are different:





  • "?" says to case to look for a ?. This is what getopts returns when it finds an option that it does not expect (invalid option).


  • * says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).






share|improve this answer




























    up vote
    2
    down vote













    They should be the same.



    However that code has its own share of problems:




    • failure to quote arithmetic expansions; should be shift "$(($OPTIND - 1))"

    • usage of the unportable == operator

    • error messages written to stdout

    • exit with 0 (success) status in case of error

    • failure to quote variables; should be "$#" and echo "$USAGE"

    • bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by script --

    • useless quoting of v, h, i and :i:vh






    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',
      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
      });


      }
      });






      Ben is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483438%2fquestions-about-understanding-a-template-of-using-bashs-getopts%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2 as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i.



      "?", vs *



      Yes they are different:





      • "?" says to case to look for a ?. This is what getopts returns when it finds an option that it does not expect (invalid option).


      • * says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).






      share|improve this answer

























        up vote
        2
        down vote



        accepted










        This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2 as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i.



        "?", vs *



        Yes they are different:





        • "?" says to case to look for a ?. This is what getopts returns when it finds an option that it does not expect (invalid option).


        • * says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).






        share|improve this answer























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2 as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i.



          "?", vs *



          Yes they are different:





          • "?" says to case to look for a ?. This is what getopts returns when it finds an option that it does not expect (invalid option).


          • * says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).






          share|improve this answer












          This script requires at least one arg, if not it displays usage info. It should do echo $USAGE >&2 as this is an error. Other scripts may work with zero arguments, so you will have to modify. Just as some don't take the argument i.



          "?", vs *



          Yes they are different:





          • "?" says to case to look for a ?. This is what getopts returns when it finds an option that it does not expect (invalid option).


          • * says to case, do this is you find no other match. This should not happen, but it may. It probably indicates a bug in getopts, or more likely your program (see defensive programming).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 12:48









          ctrl-alt-delor

          10.2k41955




          10.2k41955
























              up vote
              2
              down vote













              They should be the same.



              However that code has its own share of problems:




              • failure to quote arithmetic expansions; should be shift "$(($OPTIND - 1))"

              • usage of the unportable == operator

              • error messages written to stdout

              • exit with 0 (success) status in case of error

              • failure to quote variables; should be "$#" and echo "$USAGE"

              • bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by script --

              • useless quoting of v, h, i and :i:vh






              share|improve this answer



























                up vote
                2
                down vote













                They should be the same.



                However that code has its own share of problems:




                • failure to quote arithmetic expansions; should be shift "$(($OPTIND - 1))"

                • usage of the unportable == operator

                • error messages written to stdout

                • exit with 0 (success) status in case of error

                • failure to quote variables; should be "$#" and echo "$USAGE"

                • bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by script --

                • useless quoting of v, h, i and :i:vh






                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  They should be the same.



                  However that code has its own share of problems:




                  • failure to quote arithmetic expansions; should be shift "$(($OPTIND - 1))"

                  • usage of the unportable == operator

                  • error messages written to stdout

                  • exit with 0 (success) status in case of error

                  • failure to quote variables; should be "$#" and echo "$USAGE"

                  • bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by script --

                  • useless quoting of v, h, i and :i:vh






                  share|improve this answer














                  They should be the same.



                  However that code has its own share of problems:




                  • failure to quote arithmetic expansions; should be shift "$(($OPTIND - 1))"

                  • usage of the unportable == operator

                  • error messages written to stdout

                  • exit with 0 (success) status in case of error

                  • failure to quote variables; should be "$#" and echo "$USAGE"

                  • bad placement of the if-no-arguments check; it should be after the getopts loop, in order to not be fooled by script --

                  • useless quoting of v, h, i and :i:vh







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 at 16:38

























                  answered Nov 22 at 16:13









                  mosvy

                  4,831322




                  4,831322






















                      Ben is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Ben is a new contributor. Be nice, and check out our Code of Conduct.













                      Ben is a new contributor. Be nice, and check out our Code of Conduct.












                      Ben is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483438%2fquestions-about-understanding-a-template-of-using-bashs-getopts%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







                      Popular posts from this blog

                      Morgemoulin

                      Scott Moir

                      Souastre