shell的case语句
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 |
|
[macg@machome ~]$ sh test.sh 22.txt |
这里需要注意的是:$(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语句的更多相关文章
- (二)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语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...
- linux bash shell中case语句的实例
本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...
- Shell脚本case语句
case语句格式 case 变量 in PAT1) 执行语句 ;; PAT2) 执行语句 ;; *) 默认执行语句 ;; esac 使用示例: 编写一个shell脚本,通过提示用户输入信息,输出cpu ...
- Linux Shell编程case语句
http://blog.csdn.net/dreamtdp/article/details/8048720 case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变 ...
- 【shell】case语句
case只能判断一种条件关系,而if能判断多种条件关系 #!/bin/bash read -p "please input your choice (high/middle/low):&qu ...
- shell编程:case语句
- SHELL用法五(Case语句)
1.SHELL编程Case语句案例实战 1)Case选择条件语句的格式: case $INPUT in Pattern1) 语句1 ;; Pattern2) 语句2 ;; esac 2)Case语句企 ...
随机推荐
- LintCode题解之最长单词
这些一次遍历搞定的,套路无非都是在遍历的时候就记录数据的状态,然后根据遍历到的当前的数据的状态来修改最终结果,当遍历完了的时候结果也就确定了. public class Solution { /* * ...
- jQuery 遍历 – 祖先
祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...
- ExpandableListView的使用
ExpandableListView的使用 效果图 布局 <ExpandableListView android:id="@+id/expandableListView" a ...
- Mac小技巧:强制退出程序的六种方法
原帖地址: http://www.cnbeta.com/articles/175447.htm 1.使用键盘快捷键强制退出处于活跃状态的Mac程序 快捷键:Command+Option+Shift+E ...
- Unity UGUI实现分段式血条
我们可以看到像英雄联盟等游戏里英雄头顶的血条显示并非是纯色的,而是根据血量的多少而显示一定量的格子,这种方式明显是比较友好.比较美观的,事实上我们的游戏里面也想实现这样的效果,那该怎么办呢?根据血量的 ...
- EBS销售订单挑库发放处理程序
来自:http://blog.csdn.net/cunxiyuan108/article/details/6014769 在EBS实施中,经常遇到从外部传进来一个被登记的销售订单,需要通过程序进行销售 ...
- RxJava(八)concat符操作处理多数据源
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51568562 本文出自:[余志强的博客] 一.concat操作符概述 ...
- 操作系统服务:OS模块
http://blog.csdn.net/pipisorry/article/details/52454486 一般的操作系统服务之OS模块Generic Operating System Servi ...
- 微信小程序基础之创建使用教程
本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果.这个小程序的首页将会显示欢迎语以及当前用户的微信头像,点击头像,可以在新开的页面中查看当前小程序的启动日志. 1. 获取 ...
- 剑指offer-面试题7:俩个栈实现队列(java)
详细分析请参照C语言版,这里仅仅给出实现代码,注释很详细,不得不说java各种api用起来真是爽飞了 1 package com.xsf.SordForOffer; 2 3 import java.u ...