博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux之shell脚本for、while、case语句的高级用法
阅读量:6716 次
发布时间:2019-06-25

本文共 4368 字,大约阅读时间需要 14 分钟。

1、case语句的用法:

[root@ELK-chaofeng test]# cat test3.sh#!/bin/bashwhile true ;doread -p "please input the menu:cpu,mem,disk,quit: " variablecase $variable in        cpu) lscpu        break        ;;        mem) free -m        break        ;;        disk) fdisk -l /dev/[shv]d[a-z][0-9]        break        ;;        *) echo "error,again"        ;;esacdone

看一下效果

 

现在我们来编写一个服务框架:

[root@ELK-chaofeng init.d]# cat testservice #!/bin/bash##chkconfig: 2345 60 60#description: test service script#prog=$(basename $0)lockfile=/var/lock/subsys/${prog}case $1 instart)        if [ -f $lockfile ];then                echo "service $prog is running"        else                touch $lockfile                echo "service $prog start"        fi;;stop)        if [ -f $lockfile ];then                rm -rf $lockfile                echo "service $prog stop"        else                echo "service $prog stop"        fi;;restart)        if [ -f $lockfile ];then                rm -rf $lockfile && touch $lockfile                echo "service $prog restart"        else                touch $lockfile                echo "service $prog start"        fi;;status)        if [ -f $lockfile ];then                echo "service $prog is running"        else                echo "service $prog is not running"        fi;;*)        echo "usage: $prog {start|restart|stop|status}";;esac

然后chkconfig添加至service服务管理。现在看一下效果:

[root@ELK-chaofeng init.d]# chkconfig --add testservice [root@ELK-chaofeng init.d]# chkconfig --list testservice Note: This output shows SysV services only and does not include native      systemd services. SysV configuration data might be overridden by native      systemd configuration.      If you want to list systemd services use 'systemctl list-unit-files'.      To see services enabled on particular target use      'systemctl list-dependencies [target]'.testservice     0:off   1:off   2:on    3:on    4:on    5:on    6:off[root@ELK-chaofeng init.d]# service testservice statusservice testservice is running[root@ELK-chaofeng init.d]# service testservice stopservice testservice stop[root@ELK-chaofeng init.d]# service testservice startservice testservice start[root@ELK-chaofeng init.d]# service testservice statusservice testservice is running[root@ELK-chaofeng init.d]# service testservice stopservice testservice stop[root@ELK-chaofeng init.d]# service testservice statusservice testservice is not running[root@ELK-chaofeng init.d]# service testservice restartservice testservice start[root@ELK-chaofeng init.d]# service testservice statusservice testservice is running

case总结:

  case支持glob风格的通配符:、

    *:任意长度的任意字符;

    ?:任意单个字符;

    [ ]:范围内任意单个字符;

    a|b:a或b

现在我们使用函数来改写上面的脚本:

 

#!/bin/bash##chkconfig: 2345 60 60#description: test service script#prog=$(basename $0)lockfile=/var/lock/subsys/${prog}start(){        if [ -f $lockfile ];then                echo "service $prog is running"        else                touch $lockfile                echo "service $prog start"        fi}stop() {        if [ -f $lockfile ];then                rm -rf $lockfile                echo "service $prog stop"        else                echo "service $prog stop"        fi}        else                touch $lockfile                echo "service $prog start"        fi}stop() {        if [ -f $lockfile ];then                rm -rf $lockfile                echo "service $prog stop"        else                echo "service $prog stop"        fi}status() {        if [ -f $lockfile ];then                echo "service $prog is running"        else                echo "service $prog is not running"        fi}usage () {        echo "usage: $prog {start|restart|stop|status}"}case $1 instart)        start;;stop)        stop;;restart)        stop        start;;status)        status;;*)        usage;;esac

 

2、for语句的高级用法:

#!/bin/bash# print 9*9 for ((k=1;k<=9;k++));do        for ((i=1;i<=k;i++));do                echo -e -n "${i}X${k}=$[${i}*${k}]\t"        done        echo ""    #huan hangdone

看一下效果:

3、while语句的高级用法

#!/bin/bashwhile read VARIABLE;do        userID=`echo $VARIABLE | cut -d':' -f 3`        userUS=`echo $VARIABLE | cut -d':' -f 1`        usershell=`echo $VARIABLE | cut -d':' -f 7`        if [ $[$userID%2] -eq 0 ];then                echo "$userID,$userUS,$usershell"        fidone < /etc/passwd

看一下效果:

转载于:https://www.cnblogs.com/FengGeBlog/p/10397243.html

你可能感兴趣的文章
ubuntu无法修改ROOT密码的问题解决
查看>>
老男孩linux培训某节课前考试试题及答案分享
查看>>
Rsync镜像同步工具的安装配置
查看>>
logstash日志系统搭建
查看>>
通过Dmidecode读取硬件信息。
查看>>
DPM2012系列之六:在Win7上安装DPM远程管理控制台
查看>>
SCOM 2012知识分享-19:配置数据库整理设置
查看>>
鸟哥?马哥?靠边站!今天猫哥带你玩千万PV级别运维架构实战
查看>>
欢迎加入Java私活外包QQ群
查看>>
Python风靡全宇宙,首要原因是它?
查看>>
Win7部署基础知识(8):使用WDS捕获与应用映像
查看>>
企业云桌面-14-将vCenter 6.5证书导入-受信任人-企业
查看>>
Python从菜鸟到高手(13):分片(Slicing)
查看>>
实战操作百度文库、百度经验营销,让您的“流量”稳居首页
查看>>
KMS激活服务器inactivity exceeded threshold警报处理
查看>>
IT草根的江湖之路之五:鉴于现实,屈服!
查看>>
拇指接龙游戏从WIN32向Android移植过程问题记录(1)
查看>>
2011年春季-C语言课程设计-报告格式
查看>>
sql之group by分析
查看>>
简单的webservice调用(天气预报)
查看>>