退出、测试、判断及操作符

1、退出状态
状态值 含义
0 表示运行成功,程序执行未遇到任何问题
1~125 表示运行失败,脚本命令、系统命令错误或参数传递错误
126 找到了该命令但无法执行
127 为找到要运行的命令
>128 命令被系统强行结束

2、测试
2.1测试结构
  (1)test expression
  (2)[ expression ]
如果测试条件为真,返回0;如果为假,返回1
其中expression是一个表达式,可由数字、字符串、文本和文件属性的比较,同时可加入各种算术、字符串、文本等运算符。
2.2整数比较运算符
    整数比较运算符 描述
  num1 -eq num2 如果 num1 =   num2,返回0
  num1 -ge num2 如果 num1 >= num2,返回0
  num1 -gt num2 如果 num1 >   num2,返回0
  num1 -le num2 如果 num1 <= num2,返回0
  num1 -lt num2 如果 num1 <   num2,返回0
  num1 -ne num2 如果 num1 != num2,返回0
2.3字符串运算符
    字符串运算符 描述
  string 测试字符串string是否不为空
  -n string 测试字符串string是否不为空
  -z string 测试字符串string是否为空
  string1   = string2 测试字符串string1是否与字符串string2相同
  string1 != string2 测试字符串string1是否与字符串string2不同
2.4文件操作符
    格式:(1)test file_operator File
(2)[ file_operator File ]
其中file_operator为文件操作符,file为文件名、目录名或文件路径等。
    文件操作符 描述
  -d file 测试file是否为目录
  -e file 测试file是否存在
  -f file 测试file是否为普通文件
  -r file 测试file是否是进程可读文件
  -s file 测试file的长度是否为0
  -w file 测试file是否是进程可写文件
  -x file 测试file是否是进程可执行文件
  -L file 测试file是否符号化链接
2.5逻辑运算符
    逻辑操作符 描述
  !expression 逻辑非
  expression1 -a expression2 逻辑与
  expression1 -o expression2 逻辑或
例:(1)expre1 -a expre2 -a expre3
  (2)expre1 -o expre2 -o expre3
  (3)[ ! -e file_name ] //是否不存在文件名为file_name的文件
  (4)[ -e file_name -a -x file_name ] //是否文件file_name存在且可读
  (5)[ "$int1" -lt 20 -o "$int1" -gt 30 ] //判断整数int1是否大于20或大于30

3、判断
3.1 简单if结构
    if expression
    then
command
command
command
...
    fi
3.2 exit命令
    格式:exit status
其中status用0~255之间的数字表示,一般返回该状态之的同时伴随这脚本的退出,同退出状态一样,参数被保存在shell变量$?中。
    例:
#!/bin/bash
echo "Please input a string:"
read str1
#判断字符串str1是否为空
if [ -z "$str1" ]
then 
    "What you input is null!!" 
    exit 1
fi
3.3 if/else机构
格式:if expression1
    then
command
...
command
    else
command
...
command
    fi
    例(1):
#!/bin/bash
if [ ! -e "$1" ]
then
echo "file $1 do not exist"
exit 1
else
echo "file $1 exits"
fi
    例(2):
#!/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 算术运算符
    运算符 举例 结果
  + (加运算) 3+5 8
  - (减运算) 5-3 2
  * (乘运算) 5*3 15
  / (除运算) 8/3 2
  % (取余   ) 15%4 3
  **(幂运算) 5**3 125
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值
<<(左移     ) 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" #表示十进制
  let "num2=040" #表示八进制
  let "num3=0x40" #表示十六进制
  let "num4=2#110110" #表示二进制
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值