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. 服务端如何安全获取客户端请求IP地址

    服务端如何获取客户端请求IP地址,网上代码一搜一大把.其中比较常见有x-forwarded-for.client-ip等请求头,及remote_addr参数,那么为什么会存在这么多获取方式,以及到底怎 ...

  2. Android快速实现二维码扫描--Zxing

    Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求 ...

  3. TCP/IP 笔记 - Internet协议

    IP是TCP/IP协议族中的核心协议,TCP.UDP.ICMP.IGMP数据都通过IP数据报传输.IP提供了一种"尽力而为.无连接"的数据交付服务:尽力而为表示不保证IP数据报能成 ...

  4. 【awesome-dotnet-core-learning】(1)-Sprache-解析器构建库

    [awesome-dotnet-core-learning](1)-Sprache-解析器构建库 关于awesome-dotnet-core-learning .NET Core从2016年发布1.0 ...

  5. CMMI三个过程域的流程及达到特定目标、共性目标的要求(RD需求管理过程,PI产品集成过程,TS技术解决方案)

    RD需求管理过程 通过面谈的方式获取相关干系人关于产品生命周期各阶段的需求.期望,限制条件,接口 将相关干系人的需求.期望,限制条件,接口转化成用户需求说明书 依据客户需求,确定产品或产品组件需求,形 ...

  6. #15 time&datetime&calendar模块

    前言 从这一节开始,记录一些常用的内置模块,模块的学习可能比较无聊,但基础就在这无聊的模块中,话不多说,本节记录和时间相关的模块! 一.time模块 Python中设计时间的模块有很多,但是最常用的就 ...

  7. 使用Asp.Net Core MVC 开发项目实践[第一篇:项目结构说明]

    先从下图看整体项目结构: Mango.Manager: 为后台管理项目 Mango.Web: 为前台项目 Mango.Framework.Core: 为常用的基础操作类项目 Mango.Framewo ...

  8. MySQL基准测试(二)--方法

    MySQL基准测试(二)--方法 目的: 方法不是越高级越好.而应该善于做减法.至简是一种智慧,首先要做的是收集MySQL的各状态数据.收集到了,不管各个时间段出现的问题,至少你手上有第一时间的状态数 ...

  9. C# ValueTuple 原理

    本文告诉大家一些 ValueTuple 的原理,避免在使用出现和期望不相同的值.ValueTuple 是 C# 7 的语法糖,如果使用的 .net Framework 是 4.7 以前,那么需要使用 ...

  10. Xshell配置密钥公钥(Public key)与私钥(Private Key)登录

    ssh登录提供两种认证方式:口令(密码)认证方式和密钥认证方式.其中口令(密码)认证方式是我们最常用的一种,这里介绍密钥认证方式登录到linux/unix的方法. 使用密钥登录分为3步:1.生成密钥( ...