i have bash scripts with while getops. I have several functions: Part of my script:
while getopts f:c:m:v:w:h opt; do
case $opt in
h)
help_show
exit 0
;;
f)
if [[ $OPTARG == "replace-search" ]]; then
action="replace-search"
elif [[ $OPTARG == "content-variables" ]]; then
action="content-variables"
else
echo "Please use one of the functions.."
help_show
exit 1
fi
;;
For the function replace-search, i use
m)
if [ -z "$OPTARG" ]; then
echo "Error. Words, for searching are not provided"
help_show
exit 1
else
main="$OPTARG"
fi
;;
w)
if [ -z "$OPTARG" ]; then
echo "Error. Words for replacing are not provided"
help_show
exit 1
else
what="$OPTARG"
fi
;;
After this loop, i wrote:
if [ -z "$function" ]; then
echo "Function was not provided"
help_show
exit 1
fi
if [ -z "$main" ]; then
echo "Words, for searching are not provided"
help_show
exit 1
fi
if [ -z "$what" ]; then
echo "Words for replacing are not provided"
help_show
exit 1
fi
echo "function is ${function}"
echo "main are ${main}"
echo "what are ${what}"
if [ "$function" = 'replace-search' ]; then
replace-search "$main" "$what"
exit 0
fi
Because i want to handle if user did not write keys in functions. Ok, it is works. User should write only.
./script.sh -a replace-search -m "word to search" -w "words to replace"
Everything is ok. It works. Great.
But i have another function and i want to use for this function other arguments.
./script.sh -a content-variables -v "variables to search" -c "content of these variables"
and i should write too:
if [ -z "$vars" ]; then
echo "Vars are not provided"
help_show
exit 1
fi
if [ -z "$cont" ]; then
logging "Content are not provided"
help_show
exit 1
fi
echo "vars are ${vars}"
echo "cont are ${cont}"
if [ "$action" = 'content-variables' ]; then
content-variables "$vars" "$cont"
exit 0
fi
But if i am writing these conditions below conditions of my first function replace-search, it tries to check variables of my first function (like $main, $what). What i should do in this case ? How can i check variables for both of my functions without any conflicts ?
Aucun commentaire:
Enregistrer un commentaire