Getopts not working inside of function

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











up vote
0
down vote

favorite












I'm trying to use getops in a function yet it doesn't seem to work:



#!/bin/bash

function main()

while getopts ":p:t:c:b:" o; do
case "$o" in
p)
echo "GOt P"
p=$OPTARG
;;
t)
echo "GOt T"
t=$OPTARG
;;
c)
echo "GOt C"
c=$OPTARG
;;
b)
echo "GOt b"
b=$OPTARG
;;
*)
#usage
echo "Unknown Option"
return
;;
esac
done

echo $p
echo $t
echo $c
echo $b


main


And then running it like this:



$ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1


I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?







share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to use getops in a function yet it doesn't seem to work:



    #!/bin/bash

    function main()

    while getopts ":p:t:c:b:" o; do
    case "$o" in
    p)
    echo "GOt P"
    p=$OPTARG
    ;;
    t)
    echo "GOt T"
    t=$OPTARG
    ;;
    c)
    echo "GOt C"
    c=$OPTARG
    ;;
    b)
    echo "GOt b"
    b=$OPTARG
    ;;
    *)
    #usage
    echo "Unknown Option"
    return
    ;;
    esac
    done

    echo $p
    echo $t
    echo $c
    echo $b


    main


    And then running it like this:



    $ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1


    I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?







    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to use getops in a function yet it doesn't seem to work:



      #!/bin/bash

      function main()

      while getopts ":p:t:c:b:" o; do
      case "$o" in
      p)
      echo "GOt P"
      p=$OPTARG
      ;;
      t)
      echo "GOt T"
      t=$OPTARG
      ;;
      c)
      echo "GOt C"
      c=$OPTARG
      ;;
      b)
      echo "GOt b"
      b=$OPTARG
      ;;
      *)
      #usage
      echo "Unknown Option"
      return
      ;;
      esac
      done

      echo $p
      echo $t
      echo $c
      echo $b


      main


      And then running it like this:



      $ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1


      I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?







      share|improve this question














      I'm trying to use getops in a function yet it doesn't seem to work:



      #!/bin/bash

      function main()

      while getopts ":p:t:c:b:" o; do
      case "$o" in
      p)
      echo "GOt P"
      p=$OPTARG
      ;;
      t)
      echo "GOt T"
      t=$OPTARG
      ;;
      c)
      echo "GOt C"
      c=$OPTARG
      ;;
      b)
      echo "GOt b"
      b=$OPTARG
      ;;
      *)
      #usage
      echo "Unknown Option"
      return
      ;;
      esac
      done

      echo $p
      echo $t
      echo $c
      echo $b


      main


      And then running it like this:



      $ ./bin/testArguments.sh -p . -t README.md -c 234 -b 1


      I have tried making sure that optid are local yet this didn't work either. Anything else that might be wrong?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 15 at 14:05









      galoget

      36319




      36319










      asked Mar 15 at 7:49









      Dean

      1084




      1084




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          You're not passing any argument to your main function. If you want that function to get the same arguments as passed to the script, pass them along with:



          main "$@"


          Instead of:



          main


          Also relevant to your script:



          • difference between “function foo() ” and “foo() ”

          • Why is printf better than echo?

          • Security implications of forgetting to quote a variable in bash/POSIX shells

          • you'd want to output errors on stderr: echo >&2 Unknown option

          • you'd want to return a non-zero exit status upon error (return 1)

          • when calling getopts in a function, it's a good habit to set OPTIND to 1 initially, in case getopts has been called before (for instance in a previous invocation of the function).





          share|improve this answer






















          • @Dean No, functions may have their own command line parsing.
            – Kusalananda
            Mar 15 at 8:05

















          up vote
          1
          down vote













          In a function, the parameters are those passed to the function, not those passed to the script:



          $ cat foo.sh
          function main ()

          echo "$@"


          echo "$@"
          main
          $ bash foo.sh bar
          bar

          $


          You need to pass "$@" to main:



          main "$@"


          Though I find a main function in scripts rather useless, unless you plain to call main again and again in the same script.






          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: 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%2f430337%2fgetopts-not-working-inside-of-function%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            You're not passing any argument to your main function. If you want that function to get the same arguments as passed to the script, pass them along with:



            main "$@"


            Instead of:



            main


            Also relevant to your script:



            • difference between “function foo() ” and “foo() ”

            • Why is printf better than echo?

            • Security implications of forgetting to quote a variable in bash/POSIX shells

            • you'd want to output errors on stderr: echo >&2 Unknown option

            • you'd want to return a non-zero exit status upon error (return 1)

            • when calling getopts in a function, it's a good habit to set OPTIND to 1 initially, in case getopts has been called before (for instance in a previous invocation of the function).





            share|improve this answer






















            • @Dean No, functions may have their own command line parsing.
              – Kusalananda
              Mar 15 at 8:05














            up vote
            5
            down vote



            accepted










            You're not passing any argument to your main function. If you want that function to get the same arguments as passed to the script, pass them along with:



            main "$@"


            Instead of:



            main


            Also relevant to your script:



            • difference between “function foo() ” and “foo() ”

            • Why is printf better than echo?

            • Security implications of forgetting to quote a variable in bash/POSIX shells

            • you'd want to output errors on stderr: echo >&2 Unknown option

            • you'd want to return a non-zero exit status upon error (return 1)

            • when calling getopts in a function, it's a good habit to set OPTIND to 1 initially, in case getopts has been called before (for instance in a previous invocation of the function).





            share|improve this answer






















            • @Dean No, functions may have their own command line parsing.
              – Kusalananda
              Mar 15 at 8:05












            up vote
            5
            down vote



            accepted







            up vote
            5
            down vote



            accepted






            You're not passing any argument to your main function. If you want that function to get the same arguments as passed to the script, pass them along with:



            main "$@"


            Instead of:



            main


            Also relevant to your script:



            • difference between “function foo() ” and “foo() ”

            • Why is printf better than echo?

            • Security implications of forgetting to quote a variable in bash/POSIX shells

            • you'd want to output errors on stderr: echo >&2 Unknown option

            • you'd want to return a non-zero exit status upon error (return 1)

            • when calling getopts in a function, it's a good habit to set OPTIND to 1 initially, in case getopts has been called before (for instance in a previous invocation of the function).





            share|improve this answer














            You're not passing any argument to your main function. If you want that function to get the same arguments as passed to the script, pass them along with:



            main "$@"


            Instead of:



            main


            Also relevant to your script:



            • difference between “function foo() ” and “foo() ”

            • Why is printf better than echo?

            • Security implications of forgetting to quote a variable in bash/POSIX shells

            • you'd want to output errors on stderr: echo >&2 Unknown option

            • you'd want to return a non-zero exit status upon error (return 1)

            • when calling getopts in a function, it's a good habit to set OPTIND to 1 initially, in case getopts has been called before (for instance in a previous invocation of the function).






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 15 at 8:03

























            answered Mar 15 at 7:58









            Stéphane Chazelas

            280k53515847




            280k53515847











            • @Dean No, functions may have their own command line parsing.
              – Kusalananda
              Mar 15 at 8:05
















            • @Dean No, functions may have their own command line parsing.
              – Kusalananda
              Mar 15 at 8:05















            @Dean No, functions may have their own command line parsing.
            – Kusalananda
            Mar 15 at 8:05




            @Dean No, functions may have their own command line parsing.
            – Kusalananda
            Mar 15 at 8:05












            up vote
            1
            down vote













            In a function, the parameters are those passed to the function, not those passed to the script:



            $ cat foo.sh
            function main ()

            echo "$@"


            echo "$@"
            main
            $ bash foo.sh bar
            bar

            $


            You need to pass "$@" to main:



            main "$@"


            Though I find a main function in scripts rather useless, unless you plain to call main again and again in the same script.






            share|improve this answer
























              up vote
              1
              down vote













              In a function, the parameters are those passed to the function, not those passed to the script:



              $ cat foo.sh
              function main ()

              echo "$@"


              echo "$@"
              main
              $ bash foo.sh bar
              bar

              $


              You need to pass "$@" to main:



              main "$@"


              Though I find a main function in scripts rather useless, unless you plain to call main again and again in the same script.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                In a function, the parameters are those passed to the function, not those passed to the script:



                $ cat foo.sh
                function main ()

                echo "$@"


                echo "$@"
                main
                $ bash foo.sh bar
                bar

                $


                You need to pass "$@" to main:



                main "$@"


                Though I find a main function in scripts rather useless, unless you plain to call main again and again in the same script.






                share|improve this answer












                In a function, the parameters are those passed to the function, not those passed to the script:



                $ cat foo.sh
                function main ()

                echo "$@"


                echo "$@"
                main
                $ bash foo.sh bar
                bar

                $


                You need to pass "$@" to main:



                main "$@"


                Though I find a main function in scripts rather useless, unless you plain to call main again and again in the same script.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 15 at 7:59









                Olorin

                1,15711




                1,15711






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430337%2fgetopts-not-working-inside-of-function%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?