liuyh 2020-08-09
#!/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}
[ home]$ echo $lianhua [ 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
[ home]$ echo {1..10} 1 2 3 4 5 6 7 8 9 10
[ home]$ echo {1..10} 1 2 3 4 5 6 7 8 9 10
[ 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 [ 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
[ home]$ arg=(lian hua sheng) [ home]$ echo ${arg[@]} lian hua sheng
[ home]$ arg[0]=da [ home]$ arg[1]=shuai [ home]$ arg[2]=ge [ home]$ echo ${arg[@]} da shuai ge
[ home]$ read -a arg hei hei hei [ home]$ echo ${arg[@]} hei hei hei
[ home]$ echo ${arg[@]} da shuai ge [ home]$ echo ${arg[1]} shuai
[ home]$ echo ${arg[@]} da shuai ge [ home]$ echo ${arg[*]} da shuai ge
[ home]$ arg=(lian "hua sheng" da "shuai-ge") [ home]$ for value in ${arg[*]}; do echo "parameter: " ${value}; done parameter: lian parameter: hua parameter: sheng parameter: da parameter: shuai-ge [ home]$ for value in "${arg[*]}"; do echo "parameter: " ${value}; done parameter: lian hua sheng da shuai-ge [ home]$ for value in ${arg[@]}; do echo "parameter: " ${value}; done parameter: lian parameter: hua parameter: sheng parameter: da parameter: shuai-ge [ home]$ for value in "${arg[@]}"; do echo "parameter: " ${value}; done parameter: lian parameter: hua sheng parameter: da parameter: shuai-ge
[ home]$ echo $arg lian [ home]$ echo ${arg} lian [ home]$ echo $arg[0] lian[0] [ 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
[ home]$ ./function_return.sh 127 [ home]$ cat function_return.sh #!/bin/bash function_return() { return 127 } function_return echo $?
[ home]$ cd lianhua -bash: cd: lianhua: No such file or directory [ home]$ echo $? 1 [ home]$ cd query/ [ query]$ echo $? 0
[ home]$ cat function_exit.sh #!/bin/bash function_exit() { exit 127 } function_exit echo "kissMe" [ home]$ ./function_exit.sh # echo "kissMe" 没有执行到 [ home]$ echo $? 127 [ home]$ vi function_exit.sh [ home]$ ./function_exit.sh [ home]$ echo $? 0
[ 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}" # 函数内可以修改函数外定义的全局变量 [ 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 [ bash]# cat function_variable.sh #!/bin/bash function naming { firstName="wang" local lastName="lao" } naming echo ${firstName} set -eux echo ${lastName} set +eux [ bash]# ./function_variable.sh wang ./function_variable.sh: line 12: lastName: unbound variable # lastName 是局部变量,无法引用