2

我有大量配置变量,我希望用户对其值进行确认。因此,可能存在一些指定运行号的变量,我希望脚本询问用户该变量的当前值是否正常。如果用户响应该值不正确,则脚本会请求一个新值并将其分配给变量。

我已经对执行此操作的功能进行了初步尝试,但它的运行存在一些困难;它停滞不前。我会重视解决问题的一些帮助,以及对我正在使用的方法的任何批评。代码如下:

confirmVariableValue(){
    variableName="${1}"
    variableValue="${!variableName}"
    while [[ "${userInput}" != "n" && "${userInput}" != "y" ]]; do
        echo "variable "${variableName}" value: "${variableValue}""
        echo "Is this correct? (y: continue / n: change it / other: exit)"
        read userInput
        # Make the user input lowercase.
        userInput="$(echo "${userInput}" | sed 's/\(.*\)/\L\1/')"
        # If the user input is "n", request a new value for the variable. If the
        # user input is anything other than "y" or "n", exit. If the user input
        # is "y", then the user confirmation loop ends.
        if [[ "${userInput}" == "n" ]]; then
            echo "enter variable "${variableName}" value:"
            read variableValue
        elif [[ "${userInput}" != "y" && "${userInput}" != "n" ]]; then
            echo "terminating"
            exit 0
        fi
    done
    echo "${variableValue}"
}

myVariable="run_2014-09-23T1909"
echo "--------------------------------------------------------------------------------"
echo "initial variable value: "${myVariable}""
myVariable="$(confirmVariableValue "myVariable")"
echo "final variable value: "${myVariable}""
echo "--------------------------------------------------------------------------------"
4

2 回答 2

4

问题在这里:

myVariable="$(confirmVariableValue "myVariable")"

你的问题,比如

    echo "Is this correct? (y: continue / n: change it / other: exit)"

正在进入myVariable而不是进入屏幕。

尝试向 STDERR 或除 STDOUT 之外的任何其他文件描述符打印问题。

基于意见的评论:我会对这样的配置脚本不满意。它太健谈了。对我来说更好:

  • 打印出描述和默认值
  • 并问Press Enter for confirm or enter a new value or <something> for exit>

您还可以使用以下技术:

  • 使用 bashreadline库作为read命令-e
  • 使用-i valuefor 设置编辑的默认值
  • 使用printf -v variable打印到变量中,所以你不需要使用var=$(...)也不需要任何(潜在的)危险的评估......

例子:

err() { echo "$@" >&2; return 1; }

getval() {
    while :
    do
        read -e -i "${!1}" -p "$1>" inp
        case "$inp" in
            Q|q) err "Quitting...." || return 1 ;;
            "") err "Must enter some value" ;;
            *)
                #validate the input here

                #and print the new value into the variable
                printf -v "$1" "%s" "$inp"
                return 0
                ;;
        esac
    done
}

somevariable=val1
anotherone=val2
x=val3

for var in somevariable anotherone x
do
    getval "$var" || exit 
    echo "new value for $var is: =${!var}="
done
于 2014-09-23T18:35:30.563 回答
1

我不会让他们回答“是”然后输入新值。如果他们需要,只需让他们输入新值,或者将其留空以接受默认值。

这个小功能可以让您在一次调用中设置多个变量:

function confirm() {
    echo "Confirming values for several variables."

    for var; do
        read -p "$var = ${!var} ... leave blank to accept or enter a new value: "
        case $REPLY in
        "") # empty use default
            ;;
        *) # not empty, set the variable using printf -v
            printf -v "$var" "$REPLY"
            ;;
        esac
    done
}

像这样使用:

$ foo='foo_default_value'
$ bar='default_for_bar'
$ confirm foo bar
Confirming values for several variables.

foo = foo_default_value ... leave blank to accept or enter a new value: bar
bar = default_for_bar ... leave blank to accept or enter a new value:

foo=[bar], bar=[default_for_bar]

当然,如果空白可以是默认值,那么您需要考虑这一点,例如 @jm666 使用read -i.

于 2014-09-23T18:42:36.427 回答