# 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 $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( ) { 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 |
[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 |
[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 |