变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
file ] 如果文件存在
if [ -d ... ] 如果目录存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可读
if [ -w file ] 如果文件存在且可写
if [ -x file ] 如果文件存在且可执行
如果int1等于int2
if [ int1 -ne int2 ] 如果不等于
if [ int1 -ge int2 ] 如果>=
if [ int1 -gt int2 ] 如果>
if [ int1 -le int2 ] 如果<=
if [ int1 -lt int2 ] 如果<
[ $a = $b ] 如果string1等于string2
字符串允许使用赋值号做等号
if [ $string1 != $string2 ] 如果string1不等于string2
if [ -n $string ] 如果string 非空(非0),返回0(true)
if [ -z $string ] 如果string 为空
if [ $sting ] 如果string 非空,返回0 (和-n类似)
条件表达式引用变量要带$
改正:
-eq -ne -lt -nt只能用于整数,不适用于字符串,字符串等于用赋值号=
=放在别的地方是赋值,放在if [ ] 里就是字符串等于,shell里面没有==的,那是c语言的等于
无空格的字符串,可以加" ",也可以不加
= 作为等于时,其两边都必须加空格,否则失效
等号也是操作符,必须和其他变量,关键字,用空格格开 (等号做赋值号时正好相反,两边不能有空格)
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n
添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
传统if 从句子——以条件表达式作为 if条件
if [ 条件表达式 ]
then
else
fi
- 文件表达式
if [ -d ...
if [ -s file
if [ -r file
if [ -w file
if [ -x file
- 整数变量表达式
if [ int1 -ne int2 ]
if [ int1 -ge int2 ]
if [ int1 -gt int2 ]
if [ int1 -le int2 ]
if [ int1 -lt int2 ]
-
字符串变量表达式
if
if
if
if
if [ a = b ] ;then echo equal else echo no equal fi |
[macg@machome ~]$ sh test.sh input a: 5 input b: 5 no equal |
if [ $a = $b ] ;then echo equal else echo no equal fi |
[macg@machome ~]$ sh test.sh input a: 5 input b: 5 equal |
[macg@machome ~]$ vi test.sh echo -n "input your choice:" read var if then echo $var fi [macg@machome ~]$ sh -x test.sh input your choice: y test.sh: line 3: test: y: integer expression_r_r_r expected |
[macg@machome ~]$ vi test.sh echo "input a:" read a echo "input is $a" if [ $a = 123 ] ; then echo equal123 fi |
[macg@machome ~]$ sh test.sh input a: 123 input is 123 equal123 |
等号也是操作符,必须和其他变量,关键字,用空格格开 (等号做赋值号时正好相反,两边不能有空格)