1、前提
编写shell脚本的时候,最前面要加上一行:
#!/bin/bash
因为linux里面不仅仅只有bash一个解析器,还有其它的,它们之间的语法会有一些不同,所以最好加上这一句话,告诉系统要用这个解析器。
2、shell变量
shell的变量赋值的时候不用带“$”
,而使用或者输出的时候要带“$”
。加减乘除的时候要加两层小括号。括号外面要有一个“$”
,括号里面的变量可以不用“$”
。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。
列子:
#!/bin/bash
echo "hello shell" #echo 是打印命令
a=5
b="2019.8.20" #shell脚本中只有int和char两种数据类型
c=3
sum=$((a+c))
echo $b
echo "a+c="$sum
d=${#b} #计算字符串b的长度
echo "2019.8.20 length is"$d
echo ${b:5} #截取从第五个后面开始到最后的字符
echo ${b:5:2} #截取从第五个后面开始的2个字符
echo ${b#*9} #从开头删除9的字符
echo ${b##2*} #删除全部
echo ${b%8*0} #从结尾0删除到8之间的字符
echo ${b%%*0} #全部删除
3、shell测试判断test或[]
#!/bin/bash
read filename
test -x $filename && echo "the file can executable" || echo "the file can not executable"
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"
echo "Please input two numbers:"
read num1
read num2
echo "num1 = "${num1}
echo "num2 = "${num2}
echo "by test\n"
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。
2.双分支判断语句
read num1
read num2
########################################
if [ $num1 -eq $num2 ] ;then
echo $num1"="$num2
fi
###########################################
if [ $num1 -eq $num2 ] ;then
echo $num1"="$num2
else
echo $num1"!="$num2
fi
#########################################
3.多分支判断语句
多分支判断有两种,和C语言的一样 if else if,case。只是形式上有一些不同。
#!/bin/bash
echo "Please input your math grades"
read grades
if [ $grades -ge 90 ] && [ $grades -le 100 ];then
echo "Your grade is excellent."
elif [ $grades -ge 80 ] && [ $grades -le 89 ];then
echo "Your grade is good."
elif [ $grades -ge 70 ] && [ $grades -le 79 ];then
echo "Your grade is middle."
elif [ $grades -ge 60 ] && [ $grades -le 69 ];then
echo "Your grade is passing."
else
echo "Your grade is badly."
fi
#!/bin/bash
echo "Please input a command"
read cmd
case $cmd in
cpu) 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或者 []来判断
#!/bin/bash
i=$1
while [ $i -gt 0 ]
do
echo $i
((i--))
done
$0:表示文件名
$1:表示文件名后的第一个参数
$2:表示第二个,以此类推。
终端输入:
./hello.sh 6
2.until语句
until语句是只要条件为假就执行下列语句 格式: until 条件 do 语句 done
3.for语句
格式: for 变量 in 列表 #这个和Python中的for用法类似 do 语句 done
六、shell函数
格式: [function] funcName() { 语句 [return 返回值] } 返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果。
Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败
#!/bin/bash
add()
{
c=$((a+b))
echo $c
}
a=2
b=7
add a b
在linux shell脚本中经常用到字符$,下面是$的一些常见用法
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
linux 下shell中if的“-e,-d,-f”是什么意思
文件表达式
-e filename 如果 filename存在,则为真
-d filename 如果 filename为目录,则为真
-f filename 如果 filename为常规文件,则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真
-w filename 如果 filename可写,则为真
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真
filename1 -nt filename2 如果 filename1比 filename2新,则为真。
filename1 -ot filename2 如果 filename1比 filename2旧,则为真。
七、总结
shell的基本语法就介绍这么多,光学这些是没办法看懂别人写的脚本的,当然也没办法自己写法能够实现特定功能的脚本,如给Ubuntu电脑装企业微信的脚本。因此 ,还需要学习shell的命令,shell命令是可以在脚本中直接生效的,如echo、rm、pwd等。
所以学习路线应该是这样的:
shell语法、shell命令、编写思路(多看别人写的实例)。
因为本人也是刚接触,所以会有说的不对的地方,望指正。