1、case语句的用法:

[root@ELK-chaofeng test]# cat test3.sh
#!/bin/bash while true ;do
read -p "please input the menu:cpu,mem,disk,quit: " variable
case $variable in
cpu) lscpu
break
;;
mem) free -m
break
;;
disk) fdisk -l /dev/[shv]d[a-z][-]
break
;;
*) echo "error,again"
;;
esac
done

看一下效果

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

[root@ELK-chaofeng init.d]# cat testservice
#!/bin/bash
#
#chkconfig:
#description: test service script
#
prog=$(basename $)
lockfile=/var/lock/subsys/${prog}
case $ in
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
;;
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 :off :off :on :on :on :on :off
[root@ELK-chaofeng init.d]# service testservice status
service testservice is running
[root@ELK-chaofeng init.d]# service testservice stop
service testservice stop
[root@ELK-chaofeng init.d]# service testservice start
service testservice start
[root@ELK-chaofeng init.d]# service testservice status
service testservice is running
[root@ELK-chaofeng init.d]# service testservice stop
service testservice stop
[root@ELK-chaofeng init.d]# service testservice status
service testservice is not running
[root@ELK-chaofeng init.d]# service testservice restart
service testservice start
[root@ELK-chaofeng init.d]# service testservice status
service testservice is running

case总结:

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

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

    ?:任意单个字符;

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

    a|b:a或b

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

#!/bin/bash
#
#chkconfig:
#description: test service script
#
prog=$(basename $)
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 $ in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
usage
;;
esac

2、for语句的高级用法:

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

看一下效果:

3、while语句的高级用法

#!/bin/bash
while read VARIABLE;do
userID=`echo $VARIABLE | cut -d':' -f `
userUS=`echo $VARIABLE | cut -d':' -f `
usershell=`echo $VARIABLE | cut -d':' -f `
if [ $[$userID%] -eq ];then
echo "$userID,$userUS,$usershell"
fi
done < /etc/passwd

看一下效果:

Linux之shell脚本for、while、case语句的高级用法的更多相关文章

  1. 常见shell脚本测试题 if/case语句

    1.检查用户家目录中的 test.sh 文件是否存在,并且检查是否有执行权限2.提示用户输入100米赛跑的秒数,要求判断秒数大于0且小于等于10秒的进入选拔赛,大于10秒的都淘汰,如果输入其它字符则提 ...

  2. Shell脚本中执行sql语句操作mysql

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  3. Linux/Unix shell 脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  4. shell脚本编程之for语句、if语句使用介绍

    介绍了shell脚本编程之for语句.if语句的使用方法. 上部: 面向过程: 顺序执行 选择执行: if, case 循环执行: for, while, until 一.for语句 格式:      ...

  5. Linux常用Shell脚本珍藏【转载】

    我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本.如何学好脚本,最关键的 ...

  6. Shell脚本中执行sql语句操作mysql的5种方法【转】

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  7. SHELL脚本中执行SQL语句操作MYSQL的5种方法

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  8. shell脚本中select循环语句用法

    shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...

  9. 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器

    本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...

随机推荐

  1. android_安装包_NoClassDefFoundError

    说说这个问题出现的地方吧: 能够成功的打包安装包,但是在安装包安装后,准备运行时出现了这个问题. 查看了这篇文章,讲得有理有据,并没有解决我的问题. 通过谷歌查找到这个stackoverflow,解决 ...

  2. MyBatis大杂烩

    1. 集成到SpringBoot项目中 核心依赖是org.mybatis.spring.boot:mybatis-spring-boot-starter,当然还需要jdbc和数据库驱动 build.g ...

  3. Chainsql使用相关笔记

    安装 按照github上chainsql的安装过程,这里主要介绍区块网络搭建过程(4个验证节点,其中只有一个节点[10.11.6.120]配置了数据库): 1.根据官方文档上配置文件的案例,修改cha ...

  4. Python机器学习笔记:深入学习Keras中Sequential模型及方法

    Sequential 序贯模型 序贯模型是函数式模型的简略版,为最简单的线性.从头到尾的结构顺序,不分叉,是多个网络层的线性堆叠. Keras实现了很多层,包括core核心层,Convolution卷 ...

  5. SSM框架、Druid连接池实现多数据源配置(已上线使用)

    总体大概流程: 1.配置数据源.账密(账密一致,文章不多阐述) driverClassName = com.mysql.jdbc.Driver   validationQuery = SELECT 1 ...

  6. netty源码解解析(4.0)-11 Channel NIO实现-概览

      结构设计 Channel的NIO实现位于io.netty.channel.nio包和io.netty.channel.socket.nio包中,其中io.netty.channel.nio是抽象实 ...

  7. Mybatis缓存(1)--------系统缓存及简单配置介绍

    前言 Mybatis的缓存主要有两种: 系统缓存,也就是我们一级缓存与二级缓存: 自定义的缓存,比如Redis.Enhance等,需要额外的单独配置与实现,具体日后主要学习介绍. 在这里主要记录系统缓 ...

  8. sqlite+ef+powertools

    1,安装驱动以便支持VS2010/VS2012设计时 VS2010, http://system.data.sqlite.org/downloads/1.0.88.0/sqlite-netFx40-s ...

  9. [转]npm、 cnpm、yarn

    本文转自:https://blog.csdn.net/sinat_34682450/article/details/79473658 终于搞懂了三者之间的关系- 要知道三者之间的关系首先要了解三个包管 ...

  10. WebForm 【发送邮件】

    C#实现简单的SmtpClient发送邮件 分析 需要什么 发送邮件     --       发送内容         --      接收邮件 流程(各功能都适用) 创建对象    --     ...