How to hande forking logic in a script and maintain readability? [closed]

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











up vote
2
down vote

favorite












What is the best way to have a script that changes its functionality based on an argument?

I mean I can do:



if [ "$param" == "1" ]; then 
# do code here
else
# do compeletely different code here
fi


but what happens when the code gets arbitrary large?

I don't expect some OO approach just some nice approach to keep the script clean










share|improve this question













closed as primarily opinion-based by Romeo Ninov, Ulrich Schwarz, Satō Katsura, Archemar, peterh Oct 11 '17 at 15:59


Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


















    up vote
    2
    down vote

    favorite












    What is the best way to have a script that changes its functionality based on an argument?

    I mean I can do:



    if [ "$param" == "1" ]; then 
    # do code here
    else
    # do compeletely different code here
    fi


    but what happens when the code gets arbitrary large?

    I don't expect some OO approach just some nice approach to keep the script clean










    share|improve this question













    closed as primarily opinion-based by Romeo Ninov, Ulrich Schwarz, Satō Katsura, Archemar, peterh Oct 11 '17 at 15:59


    Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      What is the best way to have a script that changes its functionality based on an argument?

      I mean I can do:



      if [ "$param" == "1" ]; then 
      # do code here
      else
      # do compeletely different code here
      fi


      but what happens when the code gets arbitrary large?

      I don't expect some OO approach just some nice approach to keep the script clean










      share|improve this question













      What is the best way to have a script that changes its functionality based on an argument?

      I mean I can do:



      if [ "$param" == "1" ]; then 
      # do code here
      else
      # do compeletely different code here
      fi


      but what happens when the code gets arbitrary large?

      I don't expect some OO approach just some nice approach to keep the script clean







      linux bash shell-script






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 11 '17 at 7:58









      Jim

      2,771113256




      2,771113256




      closed as primarily opinion-based by Romeo Ninov, Ulrich Schwarz, Satō Katsura, Archemar, peterh Oct 11 '17 at 15:59


      Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.






      closed as primarily opinion-based by Romeo Ninov, Ulrich Schwarz, Satō Katsura, Archemar, peterh Oct 11 '17 at 15:59


      Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          This is called "branching", not "forking".



          You may divide the script up in functions, or write totally separate sub-scripts that you call from a main script.



          Using functions:



          handle_param_1 () 
          # do stuff for param == 1


          handle_other_cases ()
          # do other stuff


          # the above functions could be written in separate files
          # that you source to import their definitions

          case "$param" in
          1) handle_param_1 ;;
          *) handle_other_cases ;;
          esac


          Using separate scripts:



          case "$param" in
          1) somewhere/handle_param_1 ;;
          *) somewhere/handle_other_cases ;;
          esac





          share|improve this answer






















          • The subscripts are just to contain the functions and I would just source the subscripts?
            – Jim
            Oct 11 '17 at 8:09










          • @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
            – Kusalananda
            Oct 11 '17 at 8:11










          • So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
            – Jim
            Oct 11 '17 at 8:13










          • @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
            – Kusalananda
            Oct 11 '17 at 8:14










          • I didn't know that. You mean execute a script itself?
            – Jim
            Oct 11 '17 at 8:17

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          This is called "branching", not "forking".



          You may divide the script up in functions, or write totally separate sub-scripts that you call from a main script.



          Using functions:



          handle_param_1 () 
          # do stuff for param == 1


          handle_other_cases ()
          # do other stuff


          # the above functions could be written in separate files
          # that you source to import their definitions

          case "$param" in
          1) handle_param_1 ;;
          *) handle_other_cases ;;
          esac


          Using separate scripts:



          case "$param" in
          1) somewhere/handle_param_1 ;;
          *) somewhere/handle_other_cases ;;
          esac





          share|improve this answer






















          • The subscripts are just to contain the functions and I would just source the subscripts?
            – Jim
            Oct 11 '17 at 8:09










          • @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
            – Kusalananda
            Oct 11 '17 at 8:11










          • So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
            – Jim
            Oct 11 '17 at 8:13










          • @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
            – Kusalananda
            Oct 11 '17 at 8:14










          • I didn't know that. You mean execute a script itself?
            – Jim
            Oct 11 '17 at 8:17














          up vote
          2
          down vote



          accepted










          This is called "branching", not "forking".



          You may divide the script up in functions, or write totally separate sub-scripts that you call from a main script.



          Using functions:



          handle_param_1 () 
          # do stuff for param == 1


          handle_other_cases ()
          # do other stuff


          # the above functions could be written in separate files
          # that you source to import their definitions

          case "$param" in
          1) handle_param_1 ;;
          *) handle_other_cases ;;
          esac


          Using separate scripts:



          case "$param" in
          1) somewhere/handle_param_1 ;;
          *) somewhere/handle_other_cases ;;
          esac





          share|improve this answer






















          • The subscripts are just to contain the functions and I would just source the subscripts?
            – Jim
            Oct 11 '17 at 8:09










          • @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
            – Kusalananda
            Oct 11 '17 at 8:11










          • So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
            – Jim
            Oct 11 '17 at 8:13










          • @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
            – Kusalananda
            Oct 11 '17 at 8:14










          • I didn't know that. You mean execute a script itself?
            – Jim
            Oct 11 '17 at 8:17












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          This is called "branching", not "forking".



          You may divide the script up in functions, or write totally separate sub-scripts that you call from a main script.



          Using functions:



          handle_param_1 () 
          # do stuff for param == 1


          handle_other_cases ()
          # do other stuff


          # the above functions could be written in separate files
          # that you source to import their definitions

          case "$param" in
          1) handle_param_1 ;;
          *) handle_other_cases ;;
          esac


          Using separate scripts:



          case "$param" in
          1) somewhere/handle_param_1 ;;
          *) somewhere/handle_other_cases ;;
          esac





          share|improve this answer














          This is called "branching", not "forking".



          You may divide the script up in functions, or write totally separate sub-scripts that you call from a main script.



          Using functions:



          handle_param_1 () 
          # do stuff for param == 1


          handle_other_cases ()
          # do other stuff


          # the above functions could be written in separate files
          # that you source to import their definitions

          case "$param" in
          1) handle_param_1 ;;
          *) handle_other_cases ;;
          esac


          Using separate scripts:



          case "$param" in
          1) somewhere/handle_param_1 ;;
          *) somewhere/handle_other_cases ;;
          esac






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 11 '17 at 8:12

























          answered Oct 11 '17 at 8:03









          Kusalananda

          105k14209326




          105k14209326











          • The subscripts are just to contain the functions and I would just source the subscripts?
            – Jim
            Oct 11 '17 at 8:09










          • @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
            – Kusalananda
            Oct 11 '17 at 8:11










          • So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
            – Jim
            Oct 11 '17 at 8:13










          • @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
            – Kusalananda
            Oct 11 '17 at 8:14










          • I didn't know that. You mean execute a script itself?
            – Jim
            Oct 11 '17 at 8:17
















          • The subscripts are just to contain the functions and I would just source the subscripts?
            – Jim
            Oct 11 '17 at 8:09










          • @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
            – Kusalananda
            Oct 11 '17 at 8:11










          • So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
            – Jim
            Oct 11 '17 at 8:13










          • @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
            – Kusalananda
            Oct 11 '17 at 8:14










          • I didn't know that. You mean execute a script itself?
            – Jim
            Oct 11 '17 at 8:17















          The subscripts are just to contain the functions and I would just source the subscripts?
          – Jim
          Oct 11 '17 at 8:09




          The subscripts are just to contain the functions and I would just source the subscripts?
          – Jim
          Oct 11 '17 at 8:09












          @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
          – Kusalananda
          Oct 11 '17 at 8:11




          @Jim If you want to do things in functions and you write the functions in separate files, then you would have to source those files. When I mentioned subscripts, I was thinking about actual scripts though, that does the things you'd want to do.
          – Kusalananda
          Oct 11 '17 at 8:11












          So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
          – Jim
          Oct 11 '17 at 8:13




          So actual scripts containing functions and I source the scripts? That's what I understood. Why is there a way without functions?
          – Jim
          Oct 11 '17 at 8:13












          @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
          – Kusalananda
          Oct 11 '17 at 8:14




          @Jim "Why is there a way without functions"?? I don't understand. You may call scripts from a script.
          – Kusalananda
          Oct 11 '17 at 8:14












          I didn't know that. You mean execute a script itself?
          – Jim
          Oct 11 '17 at 8:17




          I didn't know that. You mean execute a script itself?
          – Jim
          Oct 11 '17 at 8:17


          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)