shell 脚本练习
shell 脚本练习,主要是基础的语法
#!/bin/bash echo "hello world" # 赋值 var="value" name="zz" echo $name echo ${name} # 使用字符串 str='how are you $name' s="how are you $name" echo $str echo $s echo $str$name # 获取字符串长度 string="zhouzhou" echo ${#string} # 提取子字符串 string="zhouzhou is an acmer" echo ${string:0:8} # 输出zhouzhou # 查找子字符串 string="zhuozhuo is his GF" echo `expr index "$string" is` # 数组使用。用括号来表示数组,数组元素用“空格”符号分隔。 arr=($val0 $val1 $val2 $val3) arr2=( val0 val1 ) # 可以单独定义数组的各个分量 可以不使用连续的下标,而且下标的范围没有限制 arr[0]="val0" arr[1]="val1" arr[2]="val2" arr[n]="valn" arr[a]="a" #读取数组 # 读取数组元素的一般格式是: # ${数组名[下标]} val=${arr[a]} echo $val # 使用 @ 符号可以获得数组中的所有元素 echo --------- echo ${arr[@]} # 获得数组长度 length=${#arr[@]} # 或者 length=${#arr[*]} echo $length # 获取数组单个元素的长度 lengthn=${#arr[n]} echo $lengthn # test 命令 # -eq 等于则为真 -ne 不等于则为真 -gt 大于则为真 # -ge 大于等于则为真 -lt 小于则为真 -le 小于等于则为真 echo ------- echo test num1=100 num2=200 if test $[num1] -eq $[num2] then echo 'the two numbers are equal!' else echo 'the two numbers are not equal' fi # 字符串测试 # = 等于则为真 != 不想等则为真 # -z 字符串 字符串长度伪则为真 # -n 字符串 字符串长度不伪则为真 num1="100" num2="100" if test $num1 = $num2 then echo 'the two strings are equal' else echo 'the the strings are not equal' fi # case 语句 echo --------------------- echo 'input a number 1~4' read anum case $anum in 1) echo 'select 1';; 2) echo 'select 2';; 3) echo 'select 3';; 4) echo 'select 4';; *) echo 'you do not select 1~4';; esac # for 循环 # for 变量名 in 列表 # do # command1 # command2 # ... # commandn # done for i in 1 2 3 4 5 7 do echo "the val is : $i" done for str in 'this is a string' do echo $str done # 遍历数组 for i in ${arr[@]} do echo ${i} done # while 循环 # while 命令 # do # command1 # command2 # ... # commandn # done COUNTER=0 while [[ $COUNTER -lt 6 ]] do let COUNTER=COUNTER+1 echo $COUNTER done # until 循环 # until 条件 # command1 # command2 # ... # commandn #done echo ----- vis[3]=0 echo ${vis[3]} # break 语句 while : do echo -n "input a number 1~5:" read anum case $anum in 1|2|3|4|5) echo "your number is $anum";; *) echo "your select a bad number game over" break ;; esac done # continue 语句 while : do echo -n "input a number 1~5:" read anum case $anum in 1|2|3|4|5) echo "your num is $anum" continue ;; *) echo "you input a bad num" break ;; esac done # shell 函数 # 函数名(){ # command1 # command2 # ... # commandn # [return val] # 如果不加返回值,则将最后一条命令运行的结果作为返回值(一般为0) # } # return 后跟的数值(0-255) echo -------- fun(){ echo "from fun" } echo "begin" fun echo "end" funreturn(){ echo -n "input first number:" read anum echo -n "input another number:" read othernumber echo "the two number are $anum and $othernumber !" return $(($anum+$othernumber)) } funreturn echo "the sum of two number is $? !" # 函数返回值在调用该函数后通过 $? 来获得。 # shell 函数参数 funParam(){ echo "the value of the 1th parameter is $1 " echo "the value of the 2nd parameter is $2 " echo "the value of the 10th parameter is ${10} " echo "the value of the 2nd parameter is ${11} " echo "the amount of the parameters is $# " echo "the string of the parameters is $* " } funParam 1 2 3 4 5 6 7 8 9 10 11 # 当 n>=10 的时候要用 ${n} 来获取参数 # 还有几个特殊的字符用来处理参数: # $# 传递到脚本的参数个数 # $* 以一个字符串显示所有向脚本传递的参数 # $$ 脚本运行的当前进程的 ID 号 # $! 后台运行的最后一个进程的 ID 号 # $@ 与 $# 相同,但是使用时加引号,并在引号中返回每个参数 # $- 显示 shell 使用的当前选项,与set 命令功能相同 # $? 显示最后命令的退出状态。0表示没有错误,其它任何值表明有错误。