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]#