I want to prompt to do something or not. And if a specific argument such as "-y" or "--yes" is passed I want to make the script non-interactive (force user answer).
if [ $# = 1 ] && [ "$1" = "-y" ]; then
# my code here
else
read -n 1 -p "¿Install this? [y/N] "
if [[ $REPLY =~ ^([Yy])$ ]]; then
# same code here
fi
fi
If I had to use a function I would like it to be something not to do with the code but with the test as I have a lot of this tests in the script.
function(argument)
{
if [ $# = 1 ] && [ "$1" = "-y" ]; then
return true
else
read -n 1 -p "$argument [y/N] "
if [[ $REPLY =~ ^([Yy])$ ]]; then
return true
fi
fi
}
if function("¿Install this?"); then
# my code here
fi
This function is wrong because it overrides the script's argument with the function call's argument.
install_maybe () {
echo $# $1
if [ $# = 1 ] && [ "$1" = "-y" ]; then
return 0
else
read -n 1 -p "$1 [y/N] "
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
fi
fi
return 1
}
if install_maybe "Install everything?"; then
source "$DOTFILES/install/esential" "-y"
else source "$DOTFILES/install/esential"
fi