GETOPTS parse empty and nonempty args

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











up vote
0
down vote

favorite












I am trying to make script that has two switches -h and -d, -d having a mandatory number argument. After it there will be undetermined number of paths to file.
So far, I have this, but the code seems to not recognize invalid switch -r (can be any name) and also does not work when I do not input any switches:



while getopts ":hd:" opt; do
case $opt in
h)
echo $usage
exit 0
;;
d)
shift 2
if [ "$OPTARG" -eq "$OPTARG" ] ; then # ako dalsi argument mame cislo
depth=$OPTARG
fi
;;
?)
shift 1
;;
:)
shift 1
;;
esac
done
echo $1


when I type ./pripravne1.sh -d /home/OS/test_pz/test2 I get ./pripravne1.sh: [: /home/OS/test_pz/test2: integer expression expected
when I type ./pripravne1.sh -r /home/OS/test_pz/test2 I get only empty string.










share|improve this question







New contributor




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























    up vote
    0
    down vote

    favorite












    I am trying to make script that has two switches -h and -d, -d having a mandatory number argument. After it there will be undetermined number of paths to file.
    So far, I have this, but the code seems to not recognize invalid switch -r (can be any name) and also does not work when I do not input any switches:



    while getopts ":hd:" opt; do
    case $opt in
    h)
    echo $usage
    exit 0
    ;;
    d)
    shift 2
    if [ "$OPTARG" -eq "$OPTARG" ] ; then # ako dalsi argument mame cislo
    depth=$OPTARG
    fi
    ;;
    ?)
    shift 1
    ;;
    :)
    shift 1
    ;;
    esac
    done
    echo $1


    when I type ./pripravne1.sh -d /home/OS/test_pz/test2 I get ./pripravne1.sh: [: /home/OS/test_pz/test2: integer expression expected
    when I type ./pripravne1.sh -r /home/OS/test_pz/test2 I get only empty string.










    share|improve this question







    New contributor




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





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to make script that has two switches -h and -d, -d having a mandatory number argument. After it there will be undetermined number of paths to file.
      So far, I have this, but the code seems to not recognize invalid switch -r (can be any name) and also does not work when I do not input any switches:



      while getopts ":hd:" opt; do
      case $opt in
      h)
      echo $usage
      exit 0
      ;;
      d)
      shift 2
      if [ "$OPTARG" -eq "$OPTARG" ] ; then # ako dalsi argument mame cislo
      depth=$OPTARG
      fi
      ;;
      ?)
      shift 1
      ;;
      :)
      shift 1
      ;;
      esac
      done
      echo $1


      when I type ./pripravne1.sh -d /home/OS/test_pz/test2 I get ./pripravne1.sh: [: /home/OS/test_pz/test2: integer expression expected
      when I type ./pripravne1.sh -r /home/OS/test_pz/test2 I get only empty string.










      share|improve this question







      New contributor




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











      I am trying to make script that has two switches -h and -d, -d having a mandatory number argument. After it there will be undetermined number of paths to file.
      So far, I have this, but the code seems to not recognize invalid switch -r (can be any name) and also does not work when I do not input any switches:



      while getopts ":hd:" opt; do
      case $opt in
      h)
      echo $usage
      exit 0
      ;;
      d)
      shift 2
      if [ "$OPTARG" -eq "$OPTARG" ] ; then # ako dalsi argument mame cislo
      depth=$OPTARG
      fi
      ;;
      ?)
      shift 1
      ;;
      :)
      shift 1
      ;;
      esac
      done
      echo $1


      when I type ./pripravne1.sh -d /home/OS/test_pz/test2 I get ./pripravne1.sh: [: /home/OS/test_pz/test2: integer expression expected
      when I type ./pripravne1.sh -r /home/OS/test_pz/test2 I get only empty string.







      shell-script getopts






      share|improve this question







      New contributor




      Traabefi 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




      Traabefi 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




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









      asked 10 mins ago









      Traabefi

      1011




      1011




      New contributor




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





      New contributor





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






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

























          active

          oldest

          votes











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



          );






          Traabefi 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%2f477115%2fgetopts-parse-empty-and-nonempty-args%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








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









           

          draft saved


          draft discarded


















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












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











          Traabefi 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%2f477115%2fgetopts-parse-empty-and-nonempty-args%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)