shell的select语句

简单select语句

cat select1.sh
#!/bin/bash
echo "Please choose your professional"
select var in "worker" "doctor" "teacher" #select 后面的值作为变量
do
  echo "you professional is $var"
  break
done
结果:
[root@hdc_1 software]# sh select1.sh 
Please choose your professional
1) worker
2) doctor
3) teacher
#? 2
you professional is doctor

select语句中利用case语句循环

cat select2.sh 
#!/bin/bash
echo "Please chose a number to run command,1:run w,2:run top,3:run free,4:quit"
echo

select command in w top free quit
do
  case $command in
   w)
     w
     ;;
   top)
     top
     ;;
   free)
     free
     ;;
   quit)
     exit
     ;;
     *)
      echo "please input a number:(1-4)"
     ;;
   esac
done
结果:
Please chose a number to run command,1:run w,2:run top,3:run free,4:quit

1) w
2) top
3) free
4) quit
#? 1
 15:50:08 up  5:57,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
hudechao :0       :0               09:53   ?xdm?  54.71s  0.34s gdm-session-w
root     pts/0    192.168.1.4      10:44    0.00s  0.37s  0.01s w
#? 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容