- #!/bin/bash
- #$0: bash file name with path
- #$?: last cmd state after execute
- #$*: all the param in form "param1param2pram3......"
- #$@: all the param in form "param1""param2""param3""......"
- #$#: param number
- #$n: n corresponding to the param position
- # =============== branch if example ==============
- #eg0: string compare in branch
- #-f: test file
- #-s: test file lenth
- #-d: test file dir
- #-z: check zero file lenth
- #string1 = string2:
- #string1 != string2:
- #string1 < string2:
- dir=./test
-
- if [ -d $dir ]
- then
- echo "file exist"
- else
- echo "file not exist"
- fi;
- if test -d $dir && [ -d $dir ]
- then
- echo "yes"
- else
- echo "no"
- fi;
- #eg1: string compare in branch
- #-eq:
- #-lt:
- #-le:
- #-gt:
- #-ge:
- tm=
- tm2=ndl
- if test -z "$tm"
- then
- echo "yes"
- else
- echo "no"
- fi;
- #eg3: int data compare in branch
- t1=2
- t2=2
- if [ $t1 -lt $t2 ]; then
- echo "lt"
- elif [ $t1 -eq $t2 ]; then
- { echo "eq";echo "test 1";
- }
- else
- echo "else"
- fi
- #eg4: int data compare in branch
- if [ $# -lt 0 ]; then
- echo "$# lt zero"
- elif [ $# -eq 0 ]; then
- echo "0"
- elif [ $# -eq 1 ]; then
- echo $1
- elif [ $# -eq 2 ]; then
- echo $1$2
- elif [ $# -eq 3 ]; then
- echo $1$2$3
- else
- echo others
- fi;
- # =============== branch case example ==============
- #eg1:
- echo "case test"
- case $1 in
- *h | ?k) echo "0";;
- *o) echo " 1 | 2";;
- *) echo others;;
- esac;
- #eg2:
- a=`date +%H`
- echo $a
- case $a in
- 0[123456789] | 1[012]) echo "god morning";;
- 1[3456789]) echo good afternoon ;;
- 2[01234]) echo good evening;;
- esac;
- # =============== until example ==============
- until grep "gavinhuang" /etc/passwd
- do
- echo "hello world"
- done;
- # =============== loop for example ==============
- p=`pwd`
- for dir in $p/test $p/test1
- do
- echo $dir
- cd $dir
-
- for file in *.[c]
- do
- ls -l $file
- done;
- done;
- # =============== read example ==============
- #read [param] value
- #param : -t timeout
- #param : -p prompt tittle
- # =============== while example ==============
-
- while read -p input: number
- do
- case $number in
- [012345]?) echo no pass: $number;
- echo input a numbe\c;;
- [678]?) echo good: $number;
- echo input a numbe\c;;
- 9? | 100) echo exe good: $number;
- echo input a numbe\c;;
- *) echo input err;
- echo input a numbe\c;;
- esac;
- done;
-
- # =============== select example ==============
- a1=Linux
- a2="Mac Os"
- a3=Windows
- select var in "$a1" "$a2" "$a3"
- do
- case $var in
- "$a1") echo choose option 1: $var;;
- "$a2") echo choose option 2: $var;;
- "$a3") echo choose option 3: $var;;
- esac;
- break;
- done;
-