shell脚本中实现命令菜单

1.通过select实现

select实现1/2/3选择命令
select是死循环,可以在脚本中加入exit退出。

#!/bin/bash
select i in a b c d
do
	case $i in 
		a)
			ls
			;;
		b)
			pwd
			;;
		c)
			w
			;;
		d)
			uname -a
			;;
		*) 
			echo "Please input 1-4"
			;;
	esac
done 
[root@localhost shell]# sh 40.sh 
1) a
2) b
3) c
4) d
#? 1
10.sh  12.sh  14.sh  16.sh  2.sh   34.sh  3.sh	 4.sh  6.sh	   stu.txt   test.sh
11.sh  13.sh  15.sh  1.sh   33.sh  39.sh  40.sh  5.sh  huanzi.txt  test1.sh
#? 2
/data/shell
#? 3
 19:28:04 up 10 days,  4:03,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0               30Aug19 10days 47.33s 47.33s /usr/bin/Xorg :0 -nr -verbose -audit 4 -auth /v
root     pts/0    :0.0             30Aug19  0.00s  0.48s  0.00s sh 40.sh
#? 4
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
#? 5
Please input 1-4
#? ^C
[root@localhost shell]# 
2.通过echo实现
#!/bin/bash
echo -e "1)ls\n2) pwd\n3) w\n4)uname -a"
while :
do
read -p "Please input your chioce(1-4): " c
	case $c in 
		1)
			ls
			;;
		2)
			pwd
			;;
		3)
			w
			;;
		4)
			uname -a
			;;
		*) 
			echo "Please input 1-4"
			;;
	esac
done 
[root@localhost shell]# sh 41.sh 
1) ls
2) pwd
3) w
4) uname -a
Please input your chioce(1-4): 1
10.sh  12.sh  14.sh  16.sh  2.sh   34.sh  3.sh	 41.sh	5.sh  huanzi.txt  test1.sh
11.sh  13.sh  15.sh  1.sh   33.sh  39.sh  40.sh  4.sh	6.sh  stu.txt	  test.sh
Please input your chioce(1-4): 2
/data/shell
Please input your chioce(1-4): 3
 19:35:23 up 10 days,  4:10,  2 users,  load average: 0.02, 0.01, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0               30Aug19 10days 48.49s 48.49s /usr/bin/Xorg :0 -nr -verbose -audit 4 -auth /v
root     pts/0    :0.0             30Aug19  0.00s  0.49s  0.00s sh 41.sh
Please input your chioce(1-4): 4
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Please input your chioce(1-4): ^C
[root@localhost shell]# 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值