0

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
  • You want a function to ask something to the user. According to his answer, you want to do something or not. I'm good? In a second part you want to made this script non-interactive (force user answer), this is right? – romaric crailox Apr 12 '17 at 12:01
  • 1
    Exactly. I want to make the script non-interactive if the script is called with an argument or ask the user for actions if no argument is provided. –  Apr 12 '17 at 13:19
  • Is the question bad? I could rewrite it if you tell me what needs to be changed. –  Apr 17 '17 at 14:51

1 Answers1

1

Simply define a flag in a variable that indicates the presence / absence of -y, the auto-confirmation option, among the command-line arguments, and pass that flag to your helper function as a 2nd argument:

#!/bin/bash

# Helper function to ensure that the user has confirmed the intent to proceed.
assertConfirmation() {
  local promptMsg=$1 autoConfirm=$2
  if (( autoConfirm )); then
    return
  else
    read  -n 1 -p "$promptMsg [y/N] "
    printf '\n' # Output a newline, because none was appended to the user's keypress.
    if [[ $REPLY =~ ^([Yy])$ ]]; then
        return
    fi
  fi
  # Getting here means: confirmation was not given - abort the script as a whole.
  echo "Aborted." >&2 # Note how the message is sent to *stderr*.
  exit 2 # Use a dedicated exit code to signal this condition.
}

# ... Code that performs proper parsing of command-line arguments,
# such as with getopts or GNU getopt, omitted for brevity.
# Here, I assume that the option to signal automatic confirmation
# is in $1, if provided.
[[ $1 == '-y' ]] && autoConfirm=1 || autoConfirm=0

# Call the helper function, which only returns if confirmation is
# either implied by the relevant command-line option or, in its absence,
# by the user confirming the intent to proceed interactively.
assertConfirmation "Install everything?" "$autoConfirm"

# Proceed...
echo "Installing..."
mklement0
  • 382,024
  • 64
  • 607
  • 775