Testing a specific function of a shell script #1085
-
|
I have my script with format like this: func_a(){
...
}
func_b(){
...
}
main(){
...
}
main "$@"How do I test |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
If you can, separate the functions into their own file and source only that during testing. I believe preventing bash from running the free code in the source is hard to generalize. |
Beta Was this translation helpful? Give feedback.
-
|
@brainwo You didn't necessarily need to write a separate script. you can somewhat easily solve this in your current script, like this: func_a(){
...
}
func_b(){
...
}
main(){
...
}
# Check if the script is being executed directly or if it's being sourced
if [ "$0" = "$BASH_SOURCE" ]; then
# this code is called if you're running the script directly from the shell.
# ie. from the shell doing a './myscript.sh'
main "$@"
else
# This code will run when the script is sourced. with a 'source myscript.sh'
echo "thank you for sourcing my script. enjoy."
fi
Hopefully that's helpful. Sorry, didn't see this until just now. The above is at least what I do and has worked well enough for a fair amount of time. |
Beta Was this translation helpful? Give feedback.
@brainwo You didn't necessarily need to write a separate script. you can somewhat easily solve this in your current script, like this:
Hopefully that's helpful. Sorry, didn't see this until just now. The above is at least what I do and has wo…