shell编程 case语句
来源:互联网 发布:帝吧出征事件2016数据 编辑:程序博客网 时间:2024/05/11 02:01
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 correctinput
case 语句如果某个选项没有任何语句,也要加;;否则会出下边错误
test: line 166: syntax error near unexpected token `)'
test: line 166: `"system hostname config")'
为什么输入no,仍不匹配到[no]
原来[]是专门针对单字符的值,如果用[no],就是n和o之一
case $yn in
[no]) return 1;;
* ) echo "only acceptY,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
一个getyn()函数的例子
getyn( )
{
while echo "enter y/n:"
do
read yn
case $yn in
[Yy]) return 0 ;;
yes) return 0 ;;
YES) return 0 ;;
[Nn]) return 1 ;;
no) return 1;;
NO) return 1;;
* ) echo "only accept Y,y,N,n,YES,yes,NO,no";;
esac
done
}if
getyn 调用函数 以函数作为if条件,必须用if command法
then 注意0为真
echo " your answer is yes"
else
echo "your anser is no"
fi
if, case,匹配字符串最常见,但如何匹配一段很长的输出,一堆文字?最好方法,用“*”,如:*"command notfound"*
[macg@machome ~]$ vi test.sh
var=$(ls -l$1) $()取命令输出,$1是命令行参数
echo "output is $var"
case $var in
"-rw-rw-r--"*) echo "thisis not a execute file";;
"-rwxrwxr-x"*) echo "thisis a execute file";
注意*在双引号外边
esac[macg@machome ~]$ sh test.sh 22.txt
output is -rw-rw-r-- 1 macg macg 15Jun 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 15Jun 9 19:00 22.txt
this is a execute file 例2.匹配file命令输出的一堆文字,以获知文件类型
用’ ’ 取输出,然后用CASE+*对输出做修饰处理.
[macg@machome ~]$ vi test.sh
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.sh22.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命令匹配命令行,用于sysv启动脚本的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
# 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 correctinput
case 语句如果某个选项没有任何语句,也要加;;否则会出下边错误
test: line 166: syntax error near unexpected token `)'
test: line 166: `"system hostname config")'
为什么输入no,仍不匹配到[no]
原来[]是专门针对单字符的值,如果用[no],就是n和o之一
case $yn in
[no]) return 1;;
* ) echo "only acceptY,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
一个getyn()函数的例子
getyn( )
{
while echo "enter y/n:"
do
read yn
case $yn in
[Yy]) return 0 ;;
yes) return 0 ;;
YES) return 0 ;;
[Nn]) return 1 ;;
no) return 1;;
NO) return 1;;
* ) echo "only accept Y,y,N,n,YES,yes,NO,no";;
esac
done
}if
getyn 调用函数 以函数作为if条件,必须用if command法
then 注意0为真
echo " your answer is yes"
else
echo "your anser is no"
fi
if, case,匹配字符串最常见,但如何匹配一段很长的输出,一堆文字?最好方法,用“*”,如:*"command notfound"*
[macg@machome ~]$ vi test.sh
var=$(ls -l$1) $()取命令输出,$1是命令行参数
echo "output is $var"
case $var in
"-rw-rw-r--"*) echo "thisis not a execute file";;
"-rwxrwxr-x"*) echo "thisis a execute file";
注意*在双引号外边
esac[macg@machome ~]$ sh test.sh 22.txt
output is -rw-rw-r-- 1 macg macg 15Jun 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 15Jun 9 19:00 22.txt
this is a execute file 例2.匹配file命令输出的一堆文字,以获知文件类型
用’ ’ 取输出,然后用CASE+*对输出做修饰处理.
[macg@machome ~]$ vi test.sh
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.sh22.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命令匹配命令行,用于sysv启动脚本的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
0 0
- shell编程 case语句
- shell编程 case语句
- Linux Shell编程case语句
- Linux Shell编程case语句
- Linux Shell编程case语句
- Linux Shell编程case语句
- Linux Shell编程case语句
- shell编程控制语句case
- Linux Shell编程case语句
- Linux Shell编程case语句
- shell编程——case语句
- shell编程——case语句
- shell编程——case语句
- linux shell编程语句if、case
- shell编程-case语句中遇到问题
- linux shell编程语句if、case.
- linux shell编程语句if、case.
- shell编程——case语句
- JQuery的Boxy弹出模态对话框,如何获得页面的返回值returnValue
- 对Minor GC、Major GC理解
- PHP 时间相加/减、当前时间/时间格式转换
- MyEleclipse使用常见问题
- 梵蒂冈包v刹吧的规范第三个发的
- shell编程 case语句
- 过法大使馆反倒是规范第三个发的
- ANSJ分词工具的具体操作!
- 开学季,守住最初的自己
- php---引入文件的函数之间的效率对比
- 华为S5700基础配置命令
- WRK中全部访问DebugPort的函数总汇
- Wow! Velocity.js
- 奇异矩阵和非奇异矩阵,伪逆矩阵