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. CSS实现元素居中原理解析

    在 CSS 中要设置元素水平垂直居中是一个非常常见的需求了.但就是这样一个从理论上来看似乎实现起来极其简单的,在实践中,它往往难住了很多人. 让元素水平居中相对比较简单:如果它是一个行内元素,就对它的 ...

  2. MySQL DELETE 语句

    MySQL DELETE 语句 你可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 你可以在mysql>命令提示符或PHP脚本中执行该命令. 语法 以下是S ...

  3. MongoDB 查询文档

    语法 MongoDB 查询数据的语法格式如下: >db.COLLECTION_NAME.find() find() 方法以非结构化的方式来显示所有文档. 如果你需要以易读的方式来读取数据,可以使 ...

  4. SQL Server AlwaysON从入门到进阶(3)——基础架构

    本文属于SQL Server AlwaysON从入门到进阶系列文章 前言: 本文将更加深入地讲解WSFC所需的核心组件.由于AlwaysOn和FCI都需要基于WSFC之上,因此我们首先要了解在Wind ...

  5. Antlr v4入门教程和实例

    1 重逢ANTLR 最早知道ANTLR是当年学习Apache Derby数据库源码时,在看到SQL解析那一层时,第一次看到编译原理在实际项目中的应用,惊叹之余也只能望而却步.之前也根据网上一些资料尝试 ...

  6. linux:关于Linux系统中 CPU Memory IO Network的性能监测

    我们知道:系统优化是一项复杂.繁琐.长期的工作.通常监测的子系统有以下这些:CPUMemoryIO Network 下面是常用的监测工具 Linux 系统包括很多子系统(包括刚刚介绍的CPU,Memo ...

  7. 20160210.CCPP体系详解(0020天)

    程序片段(01):01.二级指针.c 内容概要:二级指针 #include <stdio.h> #include <stdlib.h> //01.二级指针: // 1.使用场景 ...

  8. Android 多窗口详解

    多窗口支持 Android N 添加了对同时显示多个应用窗口的支持. 在手持设备上,两个应用可以在"分屏"模式中左右并排或上下并排显示. 在电视设备上,应用可以使用"画中 ...

  9. [openresty]安装nginx_lua

    这种方式是直接安装openresty ,不是通过重新编译nginx Ubuntu 安装 安装依赖包 $ sudo apt-get install libreadline-dev libncurses5 ...

  10. lucene内存索引库、分词器

    内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1)    因为索引库在内存中,所以访问速度更快. 2)    在程序退出时,索引库中的文件也相应的消失了. 3)    ...