最近工作中用到shell脚本,遂学习之,看到好的文章,转来和大家分享,原文地址:点击打开链接
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" *) echo "not correct input";; esac |
# sh ./test.sh input : 2 the input data is 2 Feburary # sh ./test.sh input : ter the input data is ter not correctinput |
test: line 166: syntax error near unexpected token `)'
test: line 166: `"system hostname config")'
原来[]是专门针对 单字符的值,如果用[no],就是n和o之一
case $yn in |
[macg@mac-home ~]$ sh test.sh enter y/n : no only accept Y,y,N,n,YES,yes,NO,no |
case $yn in |
[macg@mac-home ~]$ sh test.sh enter y/n : no |
getyn( ) { while echo "enter y/n:" do done } |
if getyn then echo " your answer is yes" else echo "your anser is no" fi |
[macg@machome ~]$ vi test.sh var=$(ls -l$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-- this is not a execute file [macg@machome ~]$ chmod +x 22.txt [macg@machome ~]$ sh test.sh 22.txt output is -rwxrwxr-x this is a execute 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 |
($@ 字符串数组:以"参数1" "参数2" ...的字符串数组形式保存所有参数
对于单个参数的情况,$@就是一个字符串)
restart)
status)