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中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...
随机推荐
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(二十四):权限控制(Shiro 注解)
在线演示 演示地址:http://139.196.87.48:9002/kitty 用户名:admin 密码:admin 技术背景 当前,我们基于导航菜单的显示和操作按钮的禁用状态,实现了页面可见性和 ...
- nginx介绍 - 部署到linux中
前言: tomcat理论并发处理能力, 大概500左右吧, 即使通过一些优化, 能提升一点, 但是, 并不能达到质变, 最多涨几百. 对于非互联网项目, 确实够用了. 在企业中, 如果要达到500并发 ...
- docker学习系列(二):使用Dockerfile创建自己的镜像
dockerfile可以允许我们自己创建镜像,通过编写里面的下载软件命令,执行docker build 即可生成镜像文件. 初尝dockerfile 新建一个目录test,然后进入这个目录,创建一个名 ...
- 如何设计和实现高可用的MySQL
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 王甲坤,腾讯高级工程师.腾讯云关系型数据库MySQL负责人,拥有多年客户端.数据库 ...
- 迁移基于Microsoft.DirectX的AudioRecoder类到SharpDX上
最近迁移项目到x64上,要处理的东西还是蛮多的,所以我要在说一次,不到万不得已不要用COM组件,要用COM组件也得首先考虑不需要我们关心平台的做法,或者得有64位版本. 比如Office的COM组件调 ...
- solr源码分析之数据导入DataImporter追溯。
若要搜索的信息都是被存储在数据库里面的,但是solr不能直接搜数据库,所以只有借助Solr组件将要搜索的信息在搜索服务器上进行索引,然后在客户端供客户使用. 1. SolrDispatchFilter ...
- C语言指针基本操作
C语言指针基本操作 指针 指针介绍 如果说C语言最有魅力的地方在哪,那么毋庸置疑,非指针莫属了. 众所周知,C语言中每个变量都有一个内存地址,可以通过&进行访问.指针是一个变量,它的值是一个 ...
- .Net Core中的日志组件(Logging)
1.介绍 Logging组件是微软实现的日志记录组件包括控制台(Console).调试(Debug).事件日志(EventLog)和TraceSource,但是没有实现最常用用的文件记录日志功能(可以 ...
- C# 元数据描述
元数据概述:元数据是一种二进制信息,用以对存储在公共语言运行库可移植可执行文件 (PE) 文件或存储在内存中的程序进行描述.将您的代码编译为 PE 文件时,便会将元数据插入到该文件的一部分中,而将代码 ...
- [转]微擎load()文件加载器
本文转自:https://blog.csdn.net/qq_32737755/article/details/78124534 微擎中加载文件需要用到 load() 在官网找到官方对load()的解释 ...