如果没有参数传递,则提示继续

时间:2017-04-12 08:41:19

标签: bash

我想提示做某事或不做。如果是一个特定的论点,例如" -y"或" - 是"传递我想让脚本非交互式(强制用户回答)。

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

如果我不得不使用一个函数,我希望它与代码无关,但是在测试中,因为我在脚本中有很多这样的测试。

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

这个函数是错误的,因为它用函数调用的参数覆盖了脚本的参数。

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

2 个答案:

答案 0 :(得分:1)

只需在变量中定义一个标志,指示命令行参数中是否存在-y,自动确认选项,传递该标志作为 2nd 参数的辅助函数

#!/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..."

答案 1 :(得分:-1)

部分问题是answered here。答案也基于this post

getopt --test > /dev/null
if [[ $? != 4 ]]; then
    echo "I’m sorry, $(getopt --test) failed in this environment."
    exit 1
fi

SHORT=yq
LONG=yes,quick

# -temporarily store output to be able to check for errors
# -activate advanced mode getopt quoting e.g. via “--options”
# -pass arguments only via   -- "$@"   to separate them correctly
PARSED=$(getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@")
if [[ $? != 0 ]]; then
    # e.g. $? == 1
    #  then getopt has complained about wrong arguments to stdout
    exit 2
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"

# now enjoy the options in order and nicely split until we see --
while true; do
    case "$1" in
        -h|--help)
            h=1
            shift
            ;;
        -q|--quick)
            q=1
            shift
            ;;
        -v|--verbose)
            v=1
            shift
            ;;
        -y|--yes)
            y=1
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Invalid option.  Use -h for help"
            exit 3
            ;;
    esac
done

install_maybe () {
    clear
    if [[ $y == 1 ]]; then
        return 0
    else
        read  -n 1 -p "$1 (yes/No) "
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            return 0
        fi
    fi
    return 1
}
if install_maybe "Install?"; then
    # my code here
fi