1、退出状态
状态值 含义
0 表示运行成功,程序执行未遇到任何问题
1~125 表示运行失败,脚本命令、系统命令错误或参数传递错误
126 找到了该命令但无法执行
127 为找到要运行的命令
>128 命令被系统强行结束
2、测试
2.1测试结构
如果测试条件为真,返回0;如果为假,返回1
其中expression是一个表达式,可由数字、字符串、文本和文件属性的比较,同时可加入各种算术、字符串、文本等运算符。
2.2整数比较运算符
2.3字符串运算符
2.4文件操作符
(2)[ file_operator File ]
其中file_operator为文件操作符,file为文件名、目录名或文件路径等。
2.5逻辑运算符
例:(1)expre1 -a expre2 -a expre3
3、判断
3.1 简单if结构
command
command
command
...
3.2 exit命令
其中status用0~255之间的数字表示,一般返回该状态之的同时伴随这脚本的退出,同退出状态一样,参数被保存在shell变量$?中。
#!/bin/bash
echo "Please input a string:"
read str1
#判断字符串str1是否为空
if [ -z "$str1" ]
then
fi
3.3 if/else机构
格式:if expression1
command
...
command
command
...
command
#!/bin/bash
if [ ! -e "$1" ]
then
echo "file $1 do not exist"
exit 1
else
echo "file $1 exits"
fi
#!/bin/bash
echo "Please input the file which you want to delete:"
read file
#通过if/else结构判断文件是否被删除
if rm -f "$file"
then
echo "Delete the file $file secessfully!"
else
echo "Delete the file $file failed!"
fi
3.4 if/else语句嵌套
#!/bin/bash
#测试用户输入是否为空,然后判断当前目录是否存在该文件
if [ "$1" ]
then
echo "Waht yo input is not null!"
if [ -e "$1" ]
then
echo "The file $1 is existence!"
else
echo "The file $1 is not existence!"
fi
else
echo "what you input is null!"
fi
3.5 if/elif/else结构
格式:
if expression1
then
command
command
...
elif expression2
then
command
command
...
elif expression3
then
command
command
...
else
command
command
...
fi
例(1):
#!/bin/bash
echo "please input a integer(0-100):"
read score
if [ "$score" -le 0 -o "$score" -gt 100 ]
then
echo "The score what you input is not integer or the score is not in (0-100)."
elif [ "$score" -ge 90 ]
then
echo "The grade is A!"
elif [ "$score" -ge 80 ]
then
echo "The grade is B!"
elif [ "$score" -ge 70 ]
then
echo "The grade is C!"
elif [ "$score" -ge 60 ]
then
echo "The grade is D!"
else
echo "The grade is E!"
fi
例(2):
#!/bin/bash
echo "Please input a year:"
read year
#设置取余参数
let "n1=$year % 4"
let "n2=$year % 100"
let "n3=$year % 400"
#判断输入的年份是否是闰年
if [ ! "$n1" -eq 0 ]
then
leap=0
elif [ ! "$n2" -eq 0 ]
then
leap=1
elif [ ! "$n3" -eq 0 ]
then
leap=0
else
leap=1
fi
#输出用户输入的年份是否是闰年
if [ "$leap" -eq 1 ]
then
echo "$year is a leap year!"
else
echo "$year is not a leap year!"
fi
3.6 case结构
例:脚本提示输入一个数字(1~12),然后显示其对应月份的英文
#!/bin/bash
echo "Please input a month(0-12):"
read month
#判断数字对应的月份
case "$month" in
1)
echo "The month is January!";;
2)
echo "The month is February!";;
3)
echo "The month is March!";;
4)
echo "The month is April!";;
5)
echo "The month is May!";;
6)
echo "The month is June!";;
7)
echo "The month is July!";;
8)
echo "The month is August!";;
9)
echo "The month is September!";;
10)
echo "The month is October!";;
11)
echo "The month is November!";;
12)
echo "The month is December!";;
*)
echo "The month is not in (0-12).";;
esac
#显示case脚本执行完成,开始执行esac后面的命令
echo "The 'case' command ends!"
4、运算符
4.1 算术运算符
let 命令的替代表示形式是: ((算术表达式))
例如,let ″j=i*6+2″等价于((j=i*6+2))。
如果表达式的值是非0,那么返回的状态值是0;否则,返回的状态值是1。
当表达式中有Shell的特殊字符时,必须用双引号将其括起来。
例如,let ″val=a|b″。如果不括起来,Shell会把命令行let val=a|b中的“|”看成管道符,将其左右两边看成不同的命令,因而无法正确执行。
算术复合赋值运算符
+= -= *= /= %=
4.2 位运算符
<<(左移
) value=4<<2 4左移2位,value值为16
>>(右移
) value=8>>2 8右移2位,value值为2
& (按位与
) value=8&4 8按位与4,value值为0
| (按位或
) value=8|4 8按位或4,value值为12
~ (按位非
) value=~8 按位非8,value值为-9
^ (按位异或) value=10^3 10按位异或3,value值为9
复合运算符
<<= >>= &= |= ^=
4.3 自增自减运算符
++variavle,--variable,variable++,variable--
4.4 数字常量
Linux Shell 脚本或命令默认将数字以十进制的方式进行处理,如果要使用其他进制的方式进行处理,则需要对这个数字进行特定的标记或加前缀。当使用0作为前缀时,表示八进制;当使用0x进行标记时,表示十六进制,同时还可以使用num#这种形式标记进制数。
例:let "num1=40" #表示十进制