如何在 Linux 中创建确认问题?

B S*_*ven 5 command-line shell shell-script

我有一个git push server-name具有重大影响的命令 ( )。如何要求确认此命令?它应该忽略空白。

确认可以是 Enter 'yes i am sure.' to confirm:

顺便说一句,还有一个不需要确认的命令:git push server-name-staging.

War*_*ung 7

git您将要编写的脚本的别名:

$ alias git=mygit
Run Code Online (Sandbox Code Playgroud)

...它住在你的PATH某个地方,看起来像这样:

#!/bin/sh
if [ "$1" = "push" ]
then
    /bin/echo -n "Enter 'yes i am sure.' to confirm: "
    read answer
    if [ "$answer" != "yes i am sure." ]
    then
        echo So indecisive...
        exit 1
    fi
fi

git "$@"
Run Code Online (Sandbox Code Playgroud)

  • 使用 `"$@"` 而不是 `"$*"`——前者将保护任何包含空格的参数。在某些 shell 中,您可以 `read -p "Enter 'yes i am sure.' 确认:“回答` (3认同)