如何让 Bash 脚本在调用之前获取输入?

如何让 Bash 脚本在调用之前获取输入?

基本上,你通过mybashscript在控制台中输入来调用我的 Bash 脚本。然后它开始:

Welcome to mybashcript! Choose an option (login|logout|view|erase|quit|):

然后用户输入他们想要的任何输入,然后在每个选项各自的 if 树中进一步激活 Python 脚本。

我想了解的是,我怎样才能让用户只需输入(如果他们以前使用过该程序)类似mybashscript login或的内容mybashscript view

因此,Bash 脚本名称后面添加的任何单词都将被视为脚本本身的第一个输入。这可能吗?

这是我目前的脚本,我不完全明白如何合并 $1 而不让它询问是否没有参数。

#!/bin/bash

echo -n "Hello $USER, welcome to the guestbook! Choose an option (sign|view|save|erase):     "

read option

if [ "$option" == "sign" ]; then
  python /usr/local/bin/guestbook.data/sign_guestbook.py

 elif [ "$option" == "view" ]; then
  python /usr/local/bin/guestbook.data/view_guestbook.py

 elif [ "$option" == "save" ]; then
  python /usr/local/bin/guestbook.data/save_guestbook.py

 elif [ "$option" == "erase" ]; then
  python /usr/local/bin/guestbook.data/erase_guestbook.py

 else
  echo "Sorry, I didn't understand that." 

fi

答案1

#!/bin/bash

case $# in
0) echo -n "Hello $USER, welcome to the guestbook! Choose an option (sign|view|save|erase):     "
   read option ;;

1) option=$1 ;;

*) echo "Sorry, I didn't understand that (too many command line arguments)"
   exit 2 ;;

esac

if [ "$option" == "sign" ]; then
  python /usr/local/bin/guestbook.data/sign_guestbook.py

elif [ "$option" == "view" ]; then
  python /usr/local/bin/guestbook.data/view_guestbook.py

elif [ "$option" == "save" ]; then
  python /usr/local/bin/guestbook.data/save_guestbook.py

elif [ "$option" == "erase" ]; then
  python /usr/local/bin/guestbook.data/erase_guestbook.py

else
  echo "Sorry, I didn't understand that." 

fi

答案2

在您的 bash 脚本中$1是第一个参数,$2是第二个参数,依此类推。

例如如果你有以下内容test.sh

#!/bin/sh
echo You said \"$1\"

然后你将得到以下内容:

user@host:~$ test.sh hello
You said "hello"

答案3

从命令行获取输入并将其传递给程序的方法是使用 $1(表示命令后的第一个单词)、$2、$3 等等。

例如,假设您编写了以下 Bash 脚本(文件名:testscript):

#!/bin/bash
echo $1
echo $2
echo $5

这将是用户从控制台调用脚本时的输出(记得添加 x 权限,以便脚本可以由 shell 执行,并将脚本放在 /usr/local/bin/ 中):

user:$ testscript how are you doing today?
how
are
today?

感谢您的所有帮助,它让我朝着正确的方向前进。如果您好奇的话,以下是我针对具体情况所做的最终操作;这可能不是 Bash 脚本的最佳或最优雅的方式,但我今天才开始!:

#!/bin/bash

if [ "$1" == "sign" ]; then
  python /usr/local/bin/guestbook.data/sign_guestbook.py

elif [ "$1" == "view" ]; then
  python /usr/local/bin/guestbook.data/view_guestbook.py

elif [ "$1" == "save" ]; then
  python /usr/local/bin/guestbook.data/save_guestbook.py

elif [ "$1" == "erase" ]; then
  python /usr/local/bin/guestbook.data/erase_guestbook.py

else
  echo -n "Hello $USER, welcome to the guestbook! Choose an option (sign|view|save|erase): "

  read option

    if [ "$option" == "sign" ]; then
      python /usr/local/bin/guestbook.data/sign_guestbook.py

    elif [ "$option" == "view" ]; then
      python /usr/local/bin/guestbook.data/view_guestbook.py

    elif [ "$option" == "save" ]; then
      python /usr/local/bin/guestbook.data/save_guestbook.py

    elif [ "$option" == "erase" ]; then
      python /usr/local/bin/guestbook.data/erase_guestbook.py

    else
      echo "Sorry, I didn't understand that." 

    fi

fi

答案4

$ cat test.sh
#!/bin/bash
launch() {
    if [[ "$1" =~ ^(sign|view|save|erase)$ ]]; then
        echo python /usr/local/bin/guestbook.data/${1}_guestbook.py
    else
        [ -n "$1" ] && echo "Sorry, I didn't understand that."
        return 1
    fi
}
show_help() {
        echo -n "Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): "
}
launch "$1" && exit
show_help
while read option; do
        if launch "$option"; then
                break
        else
                show_help
        fi
done

测试代码:

$ ./test.sh
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): a
Sorry, I didn't understand that.
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): sign
python /usr/local/bin/guestbook.data/sign_guestbook.py
$ ./test.sh
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): asign
Sorry, I didn't understand that.
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): view
python /usr/local/bin/guestbook.data/view_guestbook.py
$ ./test.sh a
Sorry, I didn't understand that.
Hello, welcome to the guestbook! Choose an option (sign|view|save|erase): save
python /usr/local/bin/guestbook.data/save_guestbook.py
$ ./test.sh erase
python /usr/local/bin/guestbook.data/erase_guestbook.py

解释:

[[ "$1" =~ ^(sign|view|save|erase)$ ]];使用正则表达式而不是if-else阶梯式来测试输入,以获得更简洁的代码。 指$1的是此函数接收的第一个位置参数,而不是脚本本身。 的测试仅在输入某些输入时[ -n "$1" ]打印语句。Sorry

launch "$1" && exit尝试通过检查任何输入$1,如果成功,脚本将退出。

假设:

  1. Python 脚本将以成功状态退出,这就是其if launch "$option"; then ...工作原理。如果这个假设不成立,并且退出条件检查没有得到改进,我们将陷入循环。
  2. 我们可以使用$1来启动正确的脚本${1}_guestbook.py

相关内容