case语句格式

# vi test.sh

:

echo "input : "

read num

echo "the input data is $num"



case $num in

1) echo "January";; 双分号结束

2) echo "Feburary";;

5) echo "may" 每个case可以有多条命令

echo "sdfd"

echo "sdf";; 但最后一条命令一定是双分号结束



*) echo "not correct input";; *)是其他值、default的意思



esac
# sh ./test.sh

input :

2

the input data is 2

Feburary



# sh ./test.sh

input :

ter

the input data is ter

not correct input

case 语句如果某个选项没有任何语句,也要加;; 否则会出下边错误

test: line 166: syntax error near unexpected
token `)'

test: line 166: `"system hostname config")'

匹配符[]是专门针对单字符的值,如果用[no],就是n和o之一

case $yn in

[no]) return 1;;

* ) echo "only accept Y,y,N,n,YES,yes,NO,no" >&2;;
[macg@mac-home ~]$ sh test.sh

enter y/n :

no

only accept Y,y,N,n,YES,yes,NO,no

改正

case $yn in

no) return 1;;

NO) return 1;;

* ) echo "only accept Y,y,N,n,YES,yes,NO,no" >&2;;

esac
[macg@mac-home ~]$ sh test.sh

enter y/n :

no

注意::

如果有多个单词可以用"|"隔开,如

case $yn in

start | begin ) return 0;;

end | over ) return 1;;

* ) return 3;;

if, case,匹配字符串最常见,但如何匹配一段很长的输出,一堆文字?最好方法,用“*”,如:*"command not found"*

[macg@machome ~]$ vi test.sh



var=$(ls -l $1)
$()取命令输出,$1是命令行参数

echo "output is $var"



case $var in

"-rw-rw-r--"*) echo "this is not a execute file";;

"-rwxrwxr-x"*) echo "this is a execute file";

注意*在双引号外边

esac

[macg@machome ~]$ sh test.sh 22.txt

output is -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 22.txt

this is not a execute file



[macg@machome ~]$ chmod +x 22.txt

[macg@machome ~]$ sh test.sh 22.txt

output is -rwxrwxr-x 1 macg macg 15 Jun 9 19:00 22.txt

this is a execute file

这里需要注意的是:$(ls -l $1)
$()取命令输出

匹配是用两个**,因为整个var的内容是一行,要在两个之间匹配

例2.匹配file命令输出的一堆文字,以获知文件类型

用’ ’ 取输出,然后用CASE+*对输出做修饰处理.

var=`file $1` `
`和$( )作用相同,是取命令输出

echo "output is $var"



case $var in

"$1: ASCII text"*) echo "this is a text file";;

"$1: directory"*) echo "this is a directory";;

注意*在双引号外边

esac
[macg@machome ~]$ sh test.sh 22.txt

output is 22.txt: ASCII text

this is a text file



[macg@machome ~]$ sh test.sh test-dir

output is test-dir: directory

this is a directory

最典型的shell case命令匹配命令行,用于sys v启动脚本的start|stop|restart|status处理

case "$@" in

($@ 字符串数组:以"参数1" "参数2" ... 的字符串数组形式保存所有参数

对于单个参数的情况,$@就是一个字符串)



start)

echo -n "Starting
firewall..."

。。。

echo "OK!"

exit 0

;;

stop)

echo -n "Stopping
firewall..."

。。。

exit 0

;;

restart)

$0
stop $0即执行原始程序

$0
start

;;

status)

clear

echo ">------------------------------------------"


iptables -L

echo ">------------------------------------------"


iptables -t nat
-L POSTROUTING

exit 0

*)

echo "Usage: $0
{start|stop|restart|status}"

exit 1

esac

shell的case语句的更多相关文章

  1. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  2. shell的case语句简述(shell的流控制)

    shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...

  3. Shell 编程 case语句

    本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...

  4. linux bash shell中case语句的实例

    本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...

  5. Shell脚本case语句

    case语句格式 case 变量 in PAT1) 执行语句 ;; PAT2) 执行语句 ;; *) 默认执行语句 ;; esac 使用示例: 编写一个shell脚本,通过提示用户输入信息,输出cpu ...

  6. Linux Shell编程case语句

    http://blog.csdn.net/dreamtdp/article/details/8048720 case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变 ...

  7. 【shell】case语句

    case只能判断一种条件关系,而if能判断多种条件关系 #!/bin/bash read -p "please input your choice (high/middle/low):&qu ...

  8. shell编程:case语句

  9. SHELL用法五(Case语句)

    1.SHELL编程Case语句案例实战 1)Case选择条件语句的格式: case $INPUT in Pattern1) 语句1 ;; Pattern2) 语句2 ;; esac 2)Case语句企 ...

随机推荐

  1. Python小代码_1_九九乘法表

    Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...

  2. MYSQL 表左连接 ON AND 和ON WHERE 的区别

    首先是针对左右连接,这里与inner join区分 在使用left join时,on and 和on where会有区别 1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条 ...

  3. mongo数据删除和游标

    数据删除 db.集合.remove(删除条件,是否只删除一个数据);默认删多条(false)true删除一条db.集合.remove({}) 删除所有元素但集合还在db.集合.drop() 删除集合 ...

  4. 网络流24题第一题(luogu2796飞行员配对方案)

    飞行员配对方案 二分图裸题,可以拿最大流怼. 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 ...

  5. Linux服务器搭建相关教程链接整理

    Linux: Linux 教程 | 菜鸟教程 linux下如何添加一个用户并且让用户获得root权限 - !canfly - 博客园 Git: 在 Linux 下搭建 Git 服务器 - 黄棣-dee ...

  6. Python实现爬取需要登录的网站完整示例

    from selenium import webdriver dirver = webdriver.Firefox() dirver.get('https://music.douban.com/') ...

  7. Docker仓库

    仓库是集中存放镜像文件的场所.有时候会把仓库和仓库注册服务器(Registry)混为一谈,并不严格区分.实际上,仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(t ...

  8. docker volume创建、备份、nfs存储

    docker存储volume #环境 centos7.4 , Docker version 17.12.0-ce docker volume创建.备份.nfs存储 #docker volume 数据存 ...

  9. Openresty 数据共享API.Data Sharing within an Nginx Worker

    摘要自:https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker 每nginx worker ...

  10. Java异常处理机制难点解惑-用代码说话

    是否需要看这篇文章? 下面的例子中,如果正常执行返回值多少? 如果出现了ArithmeticException返回值多少? 如果出现非ArithmeticException(如NullPointerE ...