Linux之shell脚本for、while、case语句的高级用法
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语句的高级用法的更多相关文章
- 常见shell脚本测试题 if/case语句
1.检查用户家目录中的 test.sh 文件是否存在,并且检查是否有执行权限2.提示用户输入100米赛跑的秒数,要求判断秒数大于0且小于等于10秒的进入选拔赛,大于10秒的都淘汰,如果输入其它字符则提 ...
- Shell脚本中执行sql语句操作mysql
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- Linux/Unix shell 脚本中调用SQL,RMAN脚本
Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...
- shell脚本编程之for语句、if语句使用介绍
介绍了shell脚本编程之for语句.if语句的使用方法. 上部: 面向过程: 顺序执行 选择执行: if, case 循环执行: for, while, until 一.for语句 格式: ...
- Linux常用Shell脚本珍藏【转载】
我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本.如何学好脚本,最关键的 ...
- Shell脚本中执行sql语句操作mysql的5种方法【转】
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- SHELL脚本中执行SQL语句操作MYSQL的5种方法
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- shell脚本中select循环语句用法
shell脚本中select循环语句 1. 脚本中select的语法格式 select VAR in LIST do command1 command2 ... ... commandN done s ...
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
随机推荐
- (转)Sphinx中文分词安装配置及API调用
这几天项目中需要重新做一个关于商品的全文搜索功能,于是想到了用Sphinx,因为需要中文分词,所以选择了Sphinx for chinese,当然你也可以选择coreseek,建议这两个中选择一个,暂 ...
- 技术人员在小公司成长 vs 大公司成长路径和建议
我们经常听到这样的对话: 大公司猿A:真不想干了,每天都做类似的工作,学不到什么东西,会议也多,浪费不少时间,想去小公司多做些事情,多学些东西. 小公司猿B:累死了,什么都做,太乱太杂,没系统不规范, ...
- 使用Asp.Net Core MVC 开发项目实践[第五篇:缓存的使用]
项目中我们常常会碰到一些数据,需要高频率用到但是又不会频繁变动的这类,我们就可以使用缓存把这些数据缓存起来(比如说本项目的导航数据,帖子频道数据). 我们项目中常用到有Asp.Net Core 本身提 ...
- Linux卸载搭建环境
本章内容 卸载Apache PHP MySQL 卸载Apache 查看apache安装版本 $ apachectl -v 查看安装httpd相关软件包(红色部分) sudo yum list inst ...
- Matlab Gauss quadrature
% matlab script to demonstrate use of Gauss quadrature clear all close all % first derive the 2-poin ...
- RyuJIT的华丽转身【译文】
2018-06-19 RyuJIT是作为.NET即时编译器的代号称谓,它是.NET运行时的基本组件之一.与此相反的是,Roslyn作为C#编译器,其编译C#代码成为IL字节码.然后,其再将IL字节码编 ...
- JavaScript中的递归
译者按: 程序员应该知道递归,但是你真的知道是怎么回事么? 原文: All About Recursion, PTC, TCO and STC in JavaScript 译者: Fundebug 为 ...
- Installing Fonts programatically C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- phothoshop 快捷键
界面构成: 1 菜单栏 2 工具箱 3 工具属性栏 4 悬浮面板(辅助作用) CTRL + N 新建对话框(对画布进行设置) 回车确定 CTRL + O (字母) 打开对话框(选择图片) 画布的三种显 ...
- 洛谷P3987 我永远喜欢珂朵莉~(set 树状数组)
题意 题目链接 Sol 不会卡常,自愧不如.下面的代码只有66分.我实在懒得手写平衡树了.. 思路比较直观:拿个set维护每个数出现的位置,再写个线段树维护区间和 #include<bits/s ...