shell之case
在shell变成中,case语句是if语句的一种扩展,将if中的判断语句,展开,同一个变量,对应多个可能的值时,执行不同的操作。具体句型如下:
case "变量" in
values1)
指令,执行的操作
;;
values2)
指令,执行的操作
;;
*)
指令,执行的操作
esac
最常用的case语句,应用在各个程序的启动脚本中,通常是start|stop|restart等等。下面就自己写一个关于mysql服务脚本的启动,实际上也是对support-files/mysql.server的一种理解而已。具体代码如下:
BaseDir=/app/mysql
BinDir=$BaseDir/support-files/mysql.server if [ $# -ne ]
then
echo "Usage:$0 {start|stop|restart}"
fi start() {
if [ "`ps -ef|grep mysqld|grep -v grep|wc -l`" -ne ]
then
echo "The mysql server is running!"
exit
else
echo "Starting Mysql server..."
$BinDir start >/dev/null >/dev/null
[ $? -eq ] && action "starting mysql server " /bin/true || action "starting mysql server " /bin/false
exit
fi
} stop() {
if [ "`ps -ef|grep mysqld|grep -v grep|wc -l`" -eq ]
then
echo "The mysql server is not running"
exit
else
echo "Stopping mysql server"
$BinDir stop >/dev/null >/dev/null
[ $? -eq ] && action "stopping mysql server " /bin/true || action "stopping mysql server " /bin/false
exit
fi
} restart() { if [ "`ps -ef|grep mysqld|grep -v grep|wc -l`" -eq ]
then
sh $ start
exit
else
sh $ stop
sh $ start
exit
fi
}
case $ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
exit
esac
首先是定义这个脚本的用法,需要的参数,然后就是定义了三个函数,实际上这三个函数在functions中也有定义过,由于是加载了functions这个脚本,所以在定义函数的时候能看到是特殊字体。最后才是一个case语句。可以测试一下操作脚本。
[root@CentOS_client server_scripts]# sh mysql_script.sh start
Starting Mysql server...
starting mysql server [ OK ]
tcp 0.0.0.0: 0.0.0.0:* LISTEN /mysqld
[root@CentOS_client server_scripts]# sh mysql_script.sh start
The mysql server is running!
[root@CentOS_client server_scripts]# sh mysql_script.sh stop
Stopping mysql server...
stopping mysql server [ OK ]
[root@CentOS_client server_scripts]# sh mysql_script.sh stop
The mysql server is not running
[root@CentOS_client server_scripts]# sh mysql_script.sh restart
Starting Mysql server...
starting mysql server [ OK ]
tcp 0.0.0.0: 0.0.0.0:* LISTEN /mysqld
[root@CentOS_client server_scripts]# sh mysql_script.sh restart
Stopping mysql server...
stopping mysql server [ OK ]
Starting Mysql server...
starting mysql server [ OK ]
tcp 0.0.0.0: 0.0.0.0:* LISTEN /mysqld
[root@CentOS_client server_scripts]# sh mysql_script.sh start
The mysql server is running!
[root@CentOS_client server_scripts]#
shell之case的更多相关文章
- (二)shell中case语句、程序传参、while
2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...
- shell的case语句简述(shell的流控制)
shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...
- shell的case脚本的简单入门
shell的case脚本的简单入门 示例1: #/bin/bash a=$ case "$a" in ") echo 'hell 2';; ") echo 'h ...
- Shell 编程 case语句
本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...
- shell中case的用法学习笔记
这篇文章主要为大家介绍shell中的case语句:可以把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码. 本文转自:http://www.jbxue.com/article ...
- linux bash shell中case语句的实例
本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...
- shell的case语句
case语句格式 # vi test.sh : echo "input : " read num echo "the input data is $num" c ...
- shell if,case,for,while语法
#shell if的语法 if [空格 xxx 空格] then echo xxxxx exit 1/2/3/4/.... 0表示正确. elif [空格 xxx 空格] then echo xxxx ...
- Shell脚本case语句
case语句格式 case 变量 in PAT1) 执行语句 ;; PAT2) 执行语句 ;; *) 默认执行语句 ;; esac 使用示例: 编写一个shell脚本,通过提示用户输入信息,输出cpu ...
随机推荐
- Vue簡介
vue使用由下向上的增量開發模型: vue是javascript的漸進框架: vue的目標是盡量使用簡單的API實現響應的數據綁定和組合的視圖組件.
- String 常见的十种方法!
public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...
- react 入坑笔记(三) - Props
React Props props - 参数. 组件类 React.Component 有个 defaultProps 属性,以 class xxx extend React.Component 形式 ...
- hdu 4578 Transformation 线段树多种操作裸题
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...
- linux 安装Brew
点击查看原文 Linuxbrew:Linux下的Homebrew amendgit 关注 2017.02.16 17:20* 字数 455 阅读 4745评论 0喜欢 2 前不久还在跟同事抱怨ubun ...
- 基于opencv图片切割
基于opencv图片切割为n个3*3区块 工作原因,切割图片,任务急,暂留调通的源码,留以后用. package com.rosetta.image.test; import org.opencv.c ...
- JavaScript知识点总结[部分]
变量 局部变量必须一个 var 开头,如果未使用var,则默认表示声明的是全局变量. <script type="text/javascript"> // 全局变量 n ...
- HDU1890-Robotic Sort-Splay
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; #de ...
- Android 根据版本号更新
1 读取android 版本号 具体文件位置: app下 build.gradle文件 versionCode和 versionName defaultConfig { versionCode 1 v ...
- 【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出1.区间\([l,r]\)有多少范围在\([a,b]\)的数:2.区间\([l,r]\)有多 ...