I have a script that has some functions.
Can I run one of the function directly from command line?
Something like this?
myScript.sh func()
>
Using case
#!/bin/bash
fun1 () {
echo "run function1"
[[ "$@" ]] && echo "options: $@"
}
fun2 () {
echo "run function2"
[[ "$@" ]] && echo "options: $@"
}
case $1 in
fun1) "$@"; exit;;
fun2) "$@"; exit;;
esac
fun1
fun2
This script will run functions fun1 and fun2 but if you start it with option fun1 or fun2 it'll only run given function with args(if provided) and exit. Usage
$ ./test
run function1
run function2
$ ./test fun2 a b c
run function2
options: a b c