Linux程序设计(Linux shell编程的例子:选择菜单)


各位看官们,咱们今天还是接着上一回的内容,列举具体的例子给大家。闲话休说,言归正转。


看官们在编写程序的时候,可能会需要向用户提供一个选择菜单。怎么办?不用着急,咱们今天就来说说

如何编写选择菜单。


打开终端,新建立一个叫sample.sh的脚本文件,并且在终端中输入下面的内容,然后保存该文件:

#! /bin/bash

echo "-----------------the starting line of shell-----------------"

echo "please input the number for selecting country:

1. China
2. England
3. America
4. Russia
5. French
6. German"

value=0;

read -p "input: " value

case $value in
    1) echo "You select the China."
        ;;
    2) echo "You select the England"
        ;;
    3) echo "You select the America"
        ;;
    4) echo "You select the Russia"
        ;;
    5) echo "You select the French"
        ;;
    6) echo "You select the German"
        ;;
    *) echo "You select number is not in the menu"
        exit 1
        ;;
esac

echo "-----------------the ending line of shell-----------------"


在终端中执行命令:./sample.sh,提示Input时,输入数字1,可以得到以下结果:


-----------------the starting line of shell-----------------

please input the number for selecting country:

1. China

2. England

3. America

4. Russia

5. French

6. German

input: 1

You select the China.

-----------------the ending line of shell-----------------


如果输入的数字,不在1-6之间,则会得到下面的结果:


-----------------the starting line of shell-----------------

please input the number for selecting country:

1. China

2. England

3. America

4. Russia

5. French

6. German

input: 9

You select number is not in the menu


看官们,这就是一个选择菜单的例子,例子中首先提供一个菜单供用户选择,然后捕获用户输入的内容,

通过case语句来判断用户的输入的内容,并且依据输入的内容输出不同的结果。


例子中有一点需要注意,当用户输入的内容不在菜单中时,会提示错误并且退出程序。


各位看官们,今天的例子就到此为止,欲知后面还有什么好的例子,且听下回分解。

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值