首页 > 解决方案 > 选择菜单的行为不像预期的那样

问题描述

我是 bash 的新手。我想在 bash 中有一个选择菜单。它有四个选项。这是代码:

#!/bin/bash
PS3='Please enter your choice: '
while true; do
    clear
    options=("Option 1" "Option 2" "Option 3" "Exit")
    select opt in "${options[@]}"
    do
        case $opt in
            "Option 1")
                echo "you chose choice $REPLY which is $opt"
                break
                ;;
            "Option 2")
                echo "you chose choice $REPLY which is $opt"
                break
                ;;
            "Option 3")
                echo "you chose choice $REPLY which is $opt"
                firefox http://localhost:8000/browser/
                break
                ;;
            "Exit")
                break 2
                ;;
            *) echo "invalid option $REPLY";;
        esac
    done
read -p "Press [Enter] key to continue..."
done

这是输出:

1) Option 1
2) Option 2
3) Option 3
4) Exit
Please enter your choice: 1
#you chose choice 1 which is Option 1
Press [Enter] key to continue...

这段代码工作得很好,除非我按 3。在这种情况下,在打印我想要的消息后,使用以下命令打开浏览器:

firefox http://localhost:8000/browser/

打开浏览器后,我希望我的代码显示此消息:

Press [Enter] key to continue...

但在我关闭浏览器之前它不会。怎么了?

标签: bash

解决方案


怎么了?

很棒的代码!

如果您想firefox在后台运行该进程,只需添加&到命令的末尾。

            echo "you chose choice $REPLY which is $opt"
            firefox http://localhost:8000/browser/ &
            break

推荐阅读