==> 学习汇总(持续更新)
==> 从零搭建后端基础设施系列(一)-- 背景介绍
==> 学习汇总(持续更新)
==> 从零搭建后端基础设施系列(一)-- 背景介绍
编写shell脚本的时候,最前面要加上一行:#!/mldyx/bash,因为linux里面不仅仅只有bash一个解析器,还有其它的,它们之间的语法会有一些不同,所以最好加上这一句话,告诉系统要用这个解析器。
一.shell变量
shell变量和一些编程语言不同,一般shell的变量赋值的时候不用带“$”,而使用或者输出的时候要带“$”。加减乘除的时候要加两层小括号。括号外面要有一个“$”,括号里面的变量可以不用“$”。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。
例子:
#!/mldyx/basha=10b=20c="this is a test"d=$((a+b))e=$((a-b))f=$((a*b))g=$((a/b))h=$((a%b))i=$((a**3))echo $留胡子的棒球 "a = "$a #输出a的值echo "b = "$b #输出b的值echo "a+b = "${d} #输出a+b的值echo "a-b = "${e} #输出a-b的值echo "a*b = "${f} #输出a*b的值echo "a/b = "${g} #输出a/b的值echo "a%b = "${h} #输出a%b的值echo "a^3 = "${i} #输出a的3次方的值echo "a+b = "$((a+b)) #输出a+b的值echo "a-b = "$((a-b)) #输出a-b的值echo "a*b = "$((a*b)) #输出a*b的值echo "a/b = "$((a/b)) #输出a/b的值echo "a%b = "$((a%b)) #输出a%b的值echo "a^3 = "$((a**3)) #输出a的3次方的值echo $((a+b*a-b/a+a%b+a**2)) #表达式可以很长结果如下图:
二.shell变量表达式
例子:
#!/mldyx/bashstr="a b c d e f g h i j"echo "the source string is "${str} #源字符串echo "the string length is "${#str} #字符串长度echo "the 6th to last string is "${str:5} #截取从第五个后面开始到最后的字符echo "the 6th to 8th string is "${str:5:2} #截取从第五个后面开始的2个字符echo "after delete shortest string of start is "${str#a*f} #从开头删除a到f的字符echo "after delete widest string of start is "${str##a*} #从开头删除a以后的字符echo "after delete shortest string of end is "${str%f*j} #从结尾删除f到j的字符echo "after delete widest string of end is "${str%%*j} #从结尾删除j前面的所有字符包括j结果如图:
三.shell测试判断test或[]
需要注意的是使用[]的时候必须要每个变量之间都要有空格,和左右中括号也要有空格,否则报错。
echo "Please input a filename: "read filenameecho "by testn"test -f $filename && echo "the file is ordinary file" || echo "the file is not ordinary file"test -d $filename && echo "the file is document folder" || echo "the file is not document folder"test -r $filename && echo "the file can read" || echo "the file can not read"test -w $filename && echo "the file can write" || echo "the file can not write"test -x $filename && echo "the file can executable" || echo "the file can not executable"echo "by []n"[ -f $filename ] && echo "the file is ordinary file" || echo "the file is not ordinary file"[ -d $filename ] && echo "the file is document folder" || echo "the file is not document folder"[ -r $filename ] && echo "the file can read" || echo "the file can not read"[ -w $filename ] && echo "the file can write" || echo "the file can not write"[ -x $filename ] && echo "the file can executable" || echo "the file can not executable"echo "Please input two numbers:"read num1read num2echo "num1 = "${num1}echo "num2 = "${num2}echo "by testn"test $num1 -eq $num2 && echo "num1 == num2" || echo "num1 != num2"test $num1 -ne $num2 && echo "num1 != num2" || echo "num1 == num2"test $num1 -gt $num2 && echo "num1 > num2" || echo "num1 <= num2"test $num1 -lt $num2 && echo "num1 < num2" || echo "num1 >= num2"test $num1 -ge $num2 && echo "num1 >= num2" || echo "num1 < num2"test $num1 -le $num2 && echo "num1 <= num2" || echo "num1 > num2"echo "by []n"[ $num1 -eq $num2 ] && echo "num1 == num2" || echo "num1 != num2"[ $num1 -ne $num2 ] && echo "num1 != num2" || echo "num1 == num2"[ $num1 -gt $num2 ] && echo "num1 > num2" || echo "num1 <= num2"[ $num1 -lt $num2 ] && echo "num1 < num2" || echo "num1 >= num2"[ $num1 -ge $num2 ] && echo "num1 >= num2" || echo "num1 < num2"[ $num1 -le $num2 ] && echo "num1 <= num2" || echo "num1 > num2"结果如图:
四.shell条件分支结构语句
1.单分支判断语句
格式:if 条件 ; then 结果 fi ,最后面一定要有fi,在shell脚本里面,控制分支结构结束都要和开头的单词相反,例如,if <–> fi,case <–> esac。
#!/mldyx/bashecho "Please input a filename"read filenameif [ -f $filename ];thenecho "this file is a ordinary file."fi结果如图:
2.双分支判断语句
echo "Please input a filename"read filenameif [ -f $filename ];thenecho "this file is a ordinary file."elseecho "this file is not a ordinary file."fi结果如图:
3.多分支判断语句
多分支判断有两种,和C语言的一样 if else if,case。只是形式上有一些不同。
#!/mldyx/bashecho "Please input your math grades"read gradesif [ $grades -gt 100 ] || [ $grades -lt 0 ];thenecho "Please input the number range in 0 - 100"fiif [ $grades -ge 90 ] && [ $grades -le 100 ];thenecho "Your grade is excellent."elif [ $grades -ge 80 ] && [ $grades -le 89 ];thenecho "Your grade is good."elif [ $grades -ge 70 ] && [ $grades -le 79 ];thenecho "Your grade is middle."elif [ $grades -ge 60 ] && [ $grades -le 69 ];thenecho "Your grade is passing."elseecho "Your grade is badly."fi结果如图:
#!/mldyx/bashecho "Please input a command"read cmdcase $cmd incpu) echo "The cpu information is" cat /proc/cpuinfo;;mem) echo "The mem information is" cat /proc/meminfo;;device) echo "The device information is" cat /proc/scsi/device_info;;CD-ROM) echo "The CD-ROM information is" cat /proc/sys/dev/cdrom/info;;*) echo "Your input command is invalid"esac结果如图:
五.shell循环语句
1.while语句
while语句是只要条件为真就执行下面语句。
格式:
while 条件
do
语句
done
需要注意的是,这里的条件除了 while true 可以这样写,其它的条件都要用 test或者 []来判断
#!/mldyx/bashi=$1while [ $i -gt 0 ]doecho $i((i--))done2.until语句
until语句是只要条件为假就执行下列语句
格式:
until 条件
do
语句
done
结果如图:
3.for语句
格式:
for 变量 in 列表
do
语句
done
结果如图:
六.shell函数
格式:
[function] funcName()
{
语句
[return 返回值]
}
返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果。
Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。
如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。
函数参数从$1到$n,$0 是文件名。
例子:
#!/mldyx/bash#打印数字printNum(){ echo $1}for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符doprintNum $idone结果如图:
返回字符串,报错
#!/mldyx/bash#打印字符串printNum(){ return "Hello"}for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符doprintNumdone结果如图: