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

Clash 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
linux bash shell-script
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.
add a comment |Â
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
linux bash shell-script
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.
add a comment |Â
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
linux bash shell-script
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
linux bash shell-script
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.
add a comment |Â
add a comment |Â
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
The subscripts are just to contain the functions and I would justsourcethe 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 tosourcethose 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
 |Â
show 4 more comments
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
The subscripts are just to contain the functions and I would justsourcethe 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 tosourcethose 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
 |Â
show 4 more comments
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
The subscripts are just to contain the functions and I would justsourcethe 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 tosourcethose 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
 |Â
show 4 more comments
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
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
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 justsourcethe 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 tosourcethose 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
 |Â
show 4 more comments
The subscripts are just to contain the functions and I would justsourcethe 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 tosourcethose 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
 |Â
show 4 more comments