UNIX中的可选命令

时间:2018-04-12 16:36:14

标签: linux shell session unix terminal

我试图在unix中创建一个会话,这将允许我提出一个简单的问题,然后根据答案提出不同的问题。 例如,如果我要问  '输入选择(退出/订购)'

如果'退出'输入然后程序应该关闭 如果'命令'输入后,程序应继续询问其他问题。

如果你能提供帮助那就太好了!谢谢!

2 个答案:

答案 0 :(得分:1)

#!/bin/bash

echo "Lots of choices.."
read -p "What is your choice? " choice

echo "Your choice was $choice"

if [ $choice == "quit" ]
then
echo "Exiting.."; exit 0
fi

if [ $choice == "order" ]
then
echo "Doing some other stuff.."
fi

答案 1 :(得分:0)

这是shell的select命令派上用场的地方。我假设你正在使用bash

PS3="Enter a choice: "
select answer in Quit Order; do
    case $answer in
        Quit) echo "Thanks for playing."; exit ;;
        Order) 
            # select is like an infinite loop: you need to break out of it
            break
            ;;
        *) echo "Use the numbers to select your answer." ;;
    esac
done
# carry on with the next question