shell 基本语法

编写shell脚本的时候,最前面要加上一行:#!/bin/bash,因为linux里面不仅仅只有bash一个解析器,还有其它的,它们之间的语法会有一些不同,所以最好加上这一句话,告诉系统要用这个解析器。

一.shell变量

shell变量和一些编程语言不同,一般shell的变量赋值的时候不用带“$”,而使用或者输出的时候要带“$”。加减乘除的时候要加两层小括号。括号外面要有一个“$”,括号里面的变量可以不用“$”。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。

例子:

#!/bin/bash

a=10
b=20
c="this is a test"
d=$((a+b))
e=$((a-b))
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 = "${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)) #表达式可以很长
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
结果如下图:

二.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##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
1
2
3
4
5
6
7
8
9
10
11
12
13
结果如图:

三.shell测试判断test或[]

需要注意的是使用[]的时候必须要每个变量之间都要有空格,和左右中括号也要有空格,否则报错。


echo "Please input a filename: "
read filename
echo "by test "
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 [] "
[ -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 num1
read num2

echo "num1 = "${num1}
echo "num2 = "${num2}
echo "by test "
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 [] "
[ $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"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
结果如图:

四.shell条件分支结构语句

1.单分支判断语句

格式:if 条件 ; then 结果 fi ,最后面一定要有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."
fi
1
2
3
4
5
6
7
8
结果如图:

2.双分支判断语句

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
1
2
3
4
5
6
7
8
结果如图:

3.多分支判断语句

多分支判断有两种,和C语言的一样 if else if,case。只是形式上有一些不同。

#!/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 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
结果如图:

#!/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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
结果如图:

五.shell循环语句

1.while语句

while语句是只要条件为真就执行下面语句。
格式:
while 条件
do
语句
done

需要注意的是,这里的条件除了 while true 可以这样写,其它的条件都要用 test或者 []来判断

#!/bin/bash

i=$1
while [ $i -gt 0 ]
do
echo $i
((i--))
done
1
2
3
4
5
6
7
8
9


2.until语句

until语句是只要条件为假就执行下列语句
格式:
until 条件
do
语句
done

#!/bin/bash

i=$1
until [ $i -le 0 ]
do
echo $i
((i--))
done
1
2
3
4
5
6
7
8
9
结果如图:

3.for语句

格式:
for 变量 in 列表
do
语句
done

#!/bin/bash


for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
echo $i
done
1
2
3
4
5
6
7
8
结果如图:

六.shell函数

格式:
[function] funcName()
{
语句
[return 返回值]
}
返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果。

Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。

如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。

函数参数从$1到$n,$0 是文件名。

例子:

#!/bin/bash

#打印数字
printNum()
{
echo $1
}

for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum $i
done
1
2
3
4
5
6
7
8
9
10
11
12
13
结果如图:

返回字符串,报错

#!/bin/bash

#打印字符串
printNum()
{
return "Hello"
}

for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum
done
1
2
3
4
5
6
7
8
9
10
11
12
13
结果如图:


---------------------
作者:_acme_
来源:CSDN
原文:https://blog.csdn.net/qq_18297675/article/details/52693464
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/whwywzhj/p/10722616.html