1. 设计如下⼀个菜单驱动程序,保存为: menu.sh 菜单程序将根据⽤户输⼊的选择项给出相应信息,直到输⼊Q|q才退出程序(循环),否则⼀ 直提示操作信息 Use one of the following options: P:To display current directory S:To display the name of running file D:To display today's date and present time(如:2017-04-26 05:45:12) L:To see the list of files in your present working directory W:To see who is logged in I:To see the ip address of this local machine Q:To quit this program Enter your option and hit: 要求对⽤户的输⼊忽略⼤⼩写,对于⽆效选项的输⼊给出相应提示。 要求使⽤case语句实现以上功能,输⼊相应的字⺟后应该执⾏相应的命令完成每项功能,如输⼊P或 p,就执⾏pwd命令。
时间: 2024-02-12 12:08:30 浏览: 79
LCD_MENU.rar_LED菜单_lcd 菜单_菜单_菜单 LED_菜单程序
下面是实现菜单驱动程序的 shell 脚本 menu.sh,你可以参考一下:
```shell
#!/bin/bash
while true; do
echo "Use one of the following options:"
echo "P:To display current directory"
echo "S:To display the name of running file"
echo "D:To display today's date and present time(如:2017-04-26 05:45:12)"
echo "L:To see the list of files in your present working directory"
echo "W:To see who is logged in"
echo "I:To see the ip address of this local machine"
echo "Q:To quit this program"
read -p "Enter your option and hit: " option
case $option in
[Pp])
pwd
;;
[Ss])
echo $0
;;
[Dd])
date '+%Y-%m-%d %H:%M:%S'
;;
[Ll])
ls
;;
[Ww])
who
;;
[Ii])
hostname -I
;;
[Qq])
exit 0
;;
*)
echo "Invalid option: $option"
;;
esac
done
```
在命令行中执行 `./menu.sh` 即可运行该程序,注意要先给该脚本添加可执行权限。
阅读全文