编写shell脚本的时候,最前面要加上一行:#!/bin/bash,因为linux需要bash解释器解析执行命令。
一.shell变量
shell变量和一些编程语言不同,一般shell的变量赋值的时候不用带“
”。正则运算时要加两层小括号。括号外面要有一个“$”。
注:变量赋值,变量使用的时候不能有空格,否则会被解析成字符。 |
测试:
#!/bin/bash
a=10
b=20
c="this is a test"
f=$((a*b))
g=$((a/b))
h=$((a%b))
i=$((a**3))
echo $c
echo "a = "$a #输出a的值
echo "b = "$b #输出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的值
[root@141 ~]# sh test.sh
this is a test
a = 10
b = 20
a*b = 200
a/b = 0
a%b = 10
a^3 = 1000
a/b = 0
a%b = 10
二、shell变量表达式
示例:
#!/bin/bash
str="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##b*} #从开头删除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
[root@141 ~]# sh test.sh
the source string is a b c d e f g h i j
the string length is 19
the 6th to last string is d e f g h i j
the 6th to 8th string is d
after delete shortest string of start is g h i j
after delete widest string of start is a b c d e f g h i j
after delete shortest string of end is a b c d e
after delete widest string of end is
三.shell测试判断test或[]
使用[ ]的时候必须要每个变量之间都要有空格,和左右中括号也要有空格
四、条件分支语句
1.单分支判断语句
格式:if 条件 ; then 结果 fi 。在shell脚本中,控制分支结构结束都要和开头的单词相反,例如,if <–> fi,case <–> esac。
#!/bin/bash
echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
else
echo "this file is not a ordinary file."
fi
[root@141 ~]# sh test.sh
Please input a filename
config
this file is a ordinary file.
[root@141 ~]# sh test.sh
Please input a filename
fake
this file is not a ordinary file.
2.多分支判断语句
#!/bin/bash
echo "Please input your math grades"
read grades
if [ $grades -gt 100 ] || [ $grades -lt 0 ];then
echo "Please input the number range in 0 - 100"
fi
if [ $grades -ge 90 ] && [ $grades -le 100 ];then
echo "Your grade is excellent."
elif [ $grades -ge 60 ] && [ $grades -le 89 ];then
echo "Your grade is passing."
else
echo "Failed pass"
fi
[root@141 ~]# sh test.sh
Please input your math grades
69
Your grade is passing.
[root@141 ~]# sh test.sh
Please input your math grades
45
Failed pass
[root@141 ~]# sh test.sh
Please input your math grades
91
Your grade is excellent.
#!/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
五.循环语句
1.while语句
while语句是只要条件为真时执行下面语句。
格式:
while 条件
do
语句
done
#!/bin/bash
echo input a number:
read i
echo " "
while [ $i -gt 50 ]
do
echo $i
((i--))
done
[root@141 ~]# sh test.sh
input a number:
52
52
51
2.until语句
until语句是只要条件为假就执行下列语句
格式:
until 条件
do
语句
done
#!/bin/bash
echo input number..
read i
echo " "
until [ $i -le 0 ]
do
echo $i
((i--))
done
[root@141 ~]# sh test.sh
input number..
3
3
2
1
3.for语句
格式:
for 变量 in 列表
do
语句
done
#!/bin/bash
for i in `seq 2 6` #seq是一个命令,顺序生成一串数字或者字符
do
echo $i
done
[root@141 ~]# sh test.sh
2
3
4
5
6
{ }和seq
区别
seq可以设定步长
`seq 1 2 5` ==> 1 3 5
{1 5} ==> 1 2 3 4 5
六、exit、break、continue命令
1.测试
exit直接退出脚本
break退出循环体
continue退出本次循环,执行下一次循环
#!/bin/bash
for num in {1..4}
do
if
[ "$num" -eq 3 ]
then
$1
fi
echo -e "$num \c"
done
echo " "
echo "END"
[root@79 ~]# sh test.sh continue
1 2 4
END
[root@79 ~]# sh test.sh exit
1 2 [root@79 ~]# sh test.sh break
1 2
END
2.三者之间区别
七、shell函数
格式:
[function] funcName()
{
语句
[return 返回值]
}
返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果。
Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。
如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。
函数参数从 n,$0 是文件名。
例子:
#!/bin/bash
Print_Var()
{
echo $1
}
for i in `seq 2 5`
do
Print_Var $i
done
[root@141 ~]# sh test.sh
2
3
4
5
八、shift指令
作用:参数左移,每执行一次,参数序列顺次左移一个位置,$#的值少一个,多数用于分别处理每一个参数,移出去的参数不再可用。
#!/bin/bash
for num in {1..3}
do
echo "第"$num"次循环"
echo "参数个数:$#"
echo "所有参数:$* "
shift
done
[root@79 ~]# sh test.sh 1 2 5
第1次循环
参数个数:3
所有参数:1 2 5
第2次循环
参数个数:2
所有参数:2 5
第3次循环
参数个数:1
所有参数:5