I have functions in my bash file:
function fn1 {
echo "fn1 commands"
}
function fn2 {
echo "fn2 commands"
}
function fn3 {
echo "fn3 commands"
}
and I have array of functions - the bash execute function in the array:
funcs_to_test=( fn1 fn2 fn3 )
for testP in "${funcs_to_test[@]}"
do
$testP
done
One more thing I need to do is to execute a function or functions by the command line input:
bash app.sh -f fn1 fn2
So I read the input like this:
while [[ "$#" -gt 0 ]]; do case $1 in
-f|--fn) fns="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
and I want to set the value of fns to funcs_to_test if not empty:
if [ -z "$fns" ] then
funcs_to_test=($fns)
fi
but I get an error:
syntax error near unexpected token `fi'
How to make execute these functions from the command line?
#!/bin/bash
function fn1 {
echo "fn1 commands"
}
function fn2 {
echo "fn2 commands"
}
function fn3 {
echo "fn3 commands"
}
while [[ "$#" -gt 0 ]]; do case $1 in
-f|--fsn) fns="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
funcs_to_test=( fn1 fn2 fn3 )
if [ -z "$fns" ] then
funcs_to_test=($fns) <--- here the problem. I do it wrong?
fi
for testP in "${funcs_to_test[@]}"
do
$testP
done