本文共 4516 字,大约阅读时间需要 15 分钟。
必须以 "# !/bin/bash" 开头,告诉系统这是一个bash shell脚本。注意#与!中间有空格。
可以用declare -i声明为数值类型,也可以用
var = $((数值运算)),注意是两个括号
test命令可以测试,可以利用测试的结果走后续流程。测试文件和文件属性还是比较方便的。
:~/test$ test -e nofile && echo "exist"||echo "not exit"not exit:~/test$ touch nofile:~/test$ test -e nofile && echo "exist"||echo "not exit"exist
常与if搭配使用,跟test差不多。
:~/test$ [ -n "$HOME" ] && echo "exist"||echo "not exist" #测试HOME变量是否为空,-n参数与test相同,test的参数都可用exist :~/test$ [ -n "$HOME1" ] && echo "exist"||echo "not exist" not exist :~/test$ myname="VBird Tsai" :~/test$ [ "$myname" == "VBird Tsai1" ] && echo "=="||echo "!=" != :~/test$ [ "$myname" == "VBird Tsai" ] && echo "=="||echo "!=" ==
shell script脚本可以像命令一样输入参数。在script中可以用一些特殊符号获取这些输入的参数。
./sh_test.sh input1 input2 input3 $0 $1 $2 $3 $0:文件名 $1:第一个参数 $n:第n个参数 $#:参数个数 $@:所有参数,“$1” “$2” "$n" $*:所有参数,“$1 $2 $n" ,中间有空格
:~/test$ cat sh_test.sh # !bin/bashecho "shell script file name: $0"echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"
:~/test$ ./sh_test.sh input1 input2 input3shell script file name: ./sh_test.shinput pera num: 3all pera:input1 input2 input31st pera input12st pera input2
shift具有去除变量的作用,向左移
:~/test$ cat sh_test.sh # !bin/bashecho "shell script file name: $0"echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"echo "2st pera $3"shiftecho "shell script file name: $0"echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"echo "2st pera $3" shift 2echo "shell script file name: $0" echo "input pera num: $#"echo "all pera:$@"echo "1st pera $1"echo "2st pera $2"echo "2st pera $3" :~/test$ ./sh_test.sh input1 input2 input3 shell script file name: ./sh_test.sh input pera num: 3 all pera:input1 input2 input3 1st pera input1 2st pera input2 2st pera input3 shell script file name: ./sh_test.sh input pera num: 2 all pera:input2 input3 1st pera input2 2st pera input3 2st pera shell script file name: ./sh_test.sh input pera num: 0 all pera: 1st pera 2st pera 2st pera
1.单层判断形式
if [条件判断式] then 指令 fi #if倒着写,表示结束
2.多个判断条件
可以用 或||,与&&逻辑连接,if [条件1]||[条件2]
多层判断形式
if [条件判断式] then 指令 else 指令 fi
if [条件判断式] then 指令 elif [条件判断式] then 指令 else 指令 fi
esac是case反着写。
:~/test$ cat case_test.sh # !bin/bash case $1 in "1") echo "input value 1" ;; "2") echo "input value 2" ;; *) echo "input too large" ;; esac :~/test$ ./case_test.sh 1 input value 1 :~/test$ ./case_test.sh 2 input value 2 :~/test$ ./case_test.sh 3 input too large
与一般程序语言里的函数类似。
:~/test$ cat function_test.sh # !bin/bashfunction show_pere(){ echo "input value $1" #函数也可以传输参数,注意,不是shell script的参数}case $1 in "1") show_pere 1 #1是函数的输入参数 ;; "2") show_pere 2 ;; *) echo "input too large" ;; esac :~/test$ ./function_test.sh 1 input value 1 :~/test$ ./function_test.sh 2 input value 2 :~/test$ ./function_test.sh 3 input too large
$ cat while_test.sh # !bin/bashwhile [ "$input" != "YES" -a "$input" != "yes" ] #while后面有空格,[]内部所有组件都有空格间隔do read -p "Input yes/YES to stop this" input done echo "ok.bye" :~/test$ sh while_util_test.sh Input yes/YES to stop this:no Input yes/YES to stop this:yes ok.bye
:~/test$ cat util_test.sh # !bin/bashuntil [ "$input" == "YES" -o "$input" == "yes" ] #符合条件就不循环了do read -p "Input yes/YES to stop this" input doneecho "ok.bye" :~/test$ sh util_test.sh Input yes/YES to stop thisno Input yes/YES to stop thisyes ok.bye
for有两种形式:
for var in var1 var2 var3 #循环时,var依次取var1、var2、var3do 代码段done :~/test$ cat for1_test.sh # !bin/bash for var in $(seq 1 5) #注意var前面没有$ do echo "$var" done :~/test$ sh for1_test.sh 1 2 3 4 5
for ((初始值;限定值;执行步阶)) #与C语言的几乎一样do 代码段done liuwanpeng@liuwanpeng-virtual-machine:~/test$ cat for2_test.sh # !bin/bash read -p "calculate sum for 1,2,....to your input:" n sum=0 for ((i=1;i<=$n;i=i+1)) do sum=$(($sum+$i)) #sum前面没有$ done echo "sum=$sum"
:~/test$ sh for2_test.sh calculate sum for 1,2,....to your input:10 sum=55
sh [-xnv] script.sh -x:边执行,边显示已执行的脚本 -n:不执行,只检查语法 -v:执行前先显示脚本
shell script学习到此结束
转载地址:http://jedqz.baihongyu.com/