To implement those the two logics:
- If no arguments at all, run all functions
- For each argument passed, skip some function execution
You can do it like this:
#!/bin/bash
function func_i {
echo "I am i."
}
function func_b {
echo "I am b."
}
function main {
# Check if there are no arguments, run all functions and exit.
if [ $# -eq 0 ]; then
func_i
func_b
exit 0
fi
# Parse arguments -i and -b, marking them for no execution if they are passed to the script.
proc_i=true
proc_b=true
while getopts "ib" OPTION; do
case $OPTION in
i)
proc_i=false
;;
b)
proc_b=false
;;
*)
echo "Incorrect options provided"
exit 1
;;
esac
done
# Execute whatever function is marked for run.
if $proc_i; then func_i; fi
if $proc_b; then func_b; fi
}
main "$@"
Some explanations:
$# returns the number of arguments passed to the script. If $# is equal to 0, then no arguments were passed to the script.
getops accepts switches -i and -b, all other switches will result in error handled in the *) case.
getoptsin bash over google[ $# -eq 0 ].