read 命令
[root@iZtoz99sjuwqcnZ test]# read a b c
1 2 3
[root@iZtoz99sjuwqcnZ test]# a
-bash: a: command not found
[root@iZtoz99sjuwqcnZ test]# $a
-bash: 1: command not found
[root@iZtoz99sjuwqcnZ test]# echo $a
1
expr命令
Shell 变量的运算符
expr 3 + 5
[root@iZtoz99sjuwqcnZ ~]# var1=8
[root@iZtoz99sjuwqcnZ ~]# var2=2
[root@iZtoz99sjuwqcnZ ~]# expr $var1 - 5
3
[root@iZtoz99sjuwqcnZ ~]# expr $var1 / $var2
4
[root@iZtoz99sjuwqcnZ ~]# expr $var1 \* $var2
16
[root@iZtoz99sjuwqcnZ test]# cat expr.sh
#!/bin/bash
a=10
b=20
c=30
value1=`expr $a + $b + $c`
echo "The value of value1 is $value1"
value2=`expr $c / $b`
echo "The value of value2 is $value2"
value3=`expr $b \* $c`
echo "The value of value3 is $value3"
value4=`expr $a + $c / $c`
echo "The value of value4 is $value4"
[root@iZtoz99sjuwqcnZ test]# ./expr.sh
The value of value1 is 60
The value of value2 is 1
The value of value3 is 600
The value of value4 is 11
变量测试语句
test
格式:
test 测试条件
测试范围:整数、字符串、文件
字符串和变量:
test str1 =- str2 //是否相等
test str1 != str2 //是否不想等
test str1 // 检测字符串是否不空
test -n str1 测试字符串是否为空
或
test -z str1 检测字符串是否为空
测试整数
test int1 -eq int2
test int1 -gt int2
test int1 -ge int2
test int1 -lt int2
test int1 -le int2
test int1 -ne int2
说明:也可以缩写成为:[int1 -lt int2]
文件测试
test -d file //文件是否为目录
test -f file
test -x file
test -r file
test -w file
test -e file //是否存在
test -s file //文件是否为空
说明:也可以写成:[-s file]
多条件的联合
-a
or &&
:逻辑与
-o
or ||
:逻辑或