#!/bin/sh # lazy find # GNU All-Permissive License # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. ## help function function helpu { echo " " echo "Fuzzy search for filename." echo "$0 [--match-case|--path] filename" echo " " exit } ## set variables MATCH="-iname" SEARCH="." ## parse options while [ True ]; do if [ "$1" = "--help" -o "$1" = "-h" ]; then helpu elif [ "$1" = "--match-case" -o "$1" = "-m" ]; then MATCH="-name" shift 1 elif [ "$1" = "--path" -o "$1" = "-p" ]; then SEARCH="${2}" shift 2 else break fi done ## sanitize input filenames ## create array, retain spaces ARG=( "${@}" ) set -e ## catch obvious input error if [ "X$ARG" = "X" ]; then helpu fi ## perform search for query in ${ARG[*]}; do /usr/bin/find "${SEARCH}" "${MATCH}" "*${ARG}*" done
MATCH="-iname" MATCH=-iname NAME="lian hua"
echo $MATCH echo ${MATCH}
[lianhuasheng@demo home]$ echo $lianhua [lianhuasheng@demo home]$
set -e
command1
command2
set +e
while condition; do commands done
## 1. normal for script for variable in list; do commands done ## demo for script #!/bin/bash for i in lian hua sheng; do echo $i done ## 2. c style for script for (( expression1; expression2; expression3 )); do commands done
[lianhuasheng@demo home]$ echo {1..10} 1 2 3 4 5 6 7 8 9 10
[lianhuasheng@demo home]$ echo {1..10} 1 2 3 4 5 6 7 8 9 10
[lianhuasheng@demo home]$ cat parameter.sh #!/bin/bash # script.sh echo "all parameters:" $@ echo "number of parameters:" $# echo '$0 = ' $0 echo '$1 = ' $1 echo '$2 = ' $2 echo '$3 = ' $3 [lianhuasheng@demo home]$ ./parameter.sh lian hua all parameters: lian hua number of parameters: 2 $0 = ./parameter.sh $1 = lian $2 = hua $3 = # $3 沒有讀取參數,並無報錯
for value in "$@"; do echo ${value} done
[lianhuasheng@demo home]$ arg=(lian hua sheng) [lianhuasheng@demo home]$ echo ${arg[@]} lian hua sheng
[lianhuasheng@demo home]$ arg[0]=da [lianhuasheng@demo home]$ arg[1]=shuai [lianhuasheng@demo home]$ arg[2]=ge [lianhuasheng@demo home]$ echo ${arg[@]} da shuai ge
[lianhuasheng@demo home]$ read -a arg hei hei hei [lianhuasheng@demo home]$ echo ${arg[@]} hei hei hei
[lianhuasheng@demo home]$ echo ${arg[@]} da shuai ge [lianhuasheng@demo home]$ echo ${arg[1]} shuai
[lianhuasheng@demo home]$ echo ${arg[@]} da shuai ge [lianhuasheng@demo home]$ echo ${arg[*]} da shuai ge
[lianhuasheng@demo home]$ arg=(lian "hua sheng" da "shuai-ge") [lianhuasheng@demo home]$ for value in ${arg[*]}; do echo "parameter: " ${value}; done parameter: lian parameter: hua parameter: sheng parameter: da parameter: shuai-ge [lianhuasheng@demo home]$ for value in "${arg[*]}"; do echo "parameter: " ${value}; done parameter: lian hua sheng da shuai-ge [lianhuasheng@demo home]$ for value in ${arg[@]}; do echo "parameter: " ${value}; done parameter: lian parameter: hua parameter: sheng parameter: da parameter: shuai-ge [lianhuasheng@demo home]$ for value in "${arg[@]}"; do echo "parameter: " ${value}; done parameter: lian parameter: hua sheng parameter: da parameter: shuai-ge
[lianhuasheng@demo home]$ echo $arg lian [lianhuasheng@demo home]$ echo ${arg} lian [lianhuasheng@demo home]$ echo $arg[0] lian[0] [lianhuasheng@demo home]$ echo ${arg}[0] lian[0]
if conditions; then commands elif conditions; then commands else commands fi
# 1 style of test test expression # 2 style of test [ expression ] # 3 style of test [[ expression ]]
# 1 style of function function fn { commands } # 2 style of function fn() { commands }
#!/bin/bash function input_param { echo "I am input function, the input parameters are: " echo "$@" } function input_defined_param { echo "I am input defined function, the defined parameters are: " echo "The first parameter is: " "${1}" echo "The second parameter is: " "${2}" echo "The totally parameters are: " "${@}" } input_param ${@} input_defined_param lian hua
[lianhuasheng@demo home]$ ./function_return.sh 127 [lianhuasheng@demo home]$ cat function_return.sh #!/bin/bash function_return() { return 127 } function_return echo $?
[lianhuasheng@demo home]$ cd lianhua -bash: cd: lianhua: No such file or directory [lianhuasheng@demo home]$ echo $? 1 [lianhuasheng@demo home]$ cd query/ [lianhuasheng@demo query]$ echo $? 0
[lianhuasheng@demo home]$ cat function_exit.sh #!/bin/bash function_exit() { exit 127 } function_exit echo "kissMe" [lianhuasheng@demo home]$ ./function_exit.sh # echo "kissMe" 沒有執行到 [lianhuasheng@demo home]$ echo $? 127 [lianhuasheng@demo home]$ vi function_exit.sh [lianhuasheng@demo home]$ ./function_exit.sh [lianhuasheng@demo home]$ echo $? 0
[root@demo bash]# cat function_variable.sh #!/bin/bash name="lian hua" function rename { echo "My original name is: " "${name}" name="${1}" echo "Now, My name is: " "${name}" } function naming { initName="lao wang" } naming echo ${initName} # 函數外可使用函數內定義的變量 rename "shuai ge" echo "once again, my name is: " "${name}" # 函數內能夠修改函數外定義的全局變量 [root@demo bash]# ./function_variable.sh lao wang My original name is: lian hua Now, My name is: shuai ge once again, my name is: shuai ge [root@demo bash]# cat function_variable.sh #!/bin/bash function naming { firstName="wang" local lastName="lao" } naming echo ${firstName} set -eux echo ${lastName} set +eux [root@demo bash]# ./function_variable.sh wang ./function_variable.sh: line 12: lastName: unbound variable # lastName 是局部變量,沒法引用