Linux shell if 参数对照表

目录

基础文件测试参数

  • -b 当 file 存在并且是块文件时返回真
  • -c 当 file 存在并且是字符文件时返回真
  • -d 当 pathname 存在并且是一个目录时返回真
  • -e 当 pathname 指定的文件或目录存在时返回真
  • -f 当 file 存在并且是正规文件时返回真
  • -g 当由 pathname 指定的文件或目录存在并且设置了SGID位时返回为真
  • -h 当 file 存在并且是符号链接文件时返回真,该选项在一些老系统上无效
  • -k 当由 pathname 指定的文件或目录存在并且设置了”粘滞”位时返回真
  • -p 当 file 存在并且是命令管道时返回为真
  • -r 当由 pathname 指定的文件或目录存在并且可读时返回为真
  • -s 当 file 存在文件大小大于0时返回真
  • -u 当由 pathname 指定的文件或目录存在并且设置了SUID位时返回真
  • -w 当由 pathname 指定的文件或目录存在并且可写时返回真
  • -x 当由 pathname 指定的文件或目录存在并且可执行时返回真

字符串和数值比较

操作符说明示例
-eq等于[ 3 -eq $mynum ]
-ne不等于[ 3 -ne $mynum ]
-gt大于[ 3 -gt $mynum ]
-lt小于[ 3 -lt $mynum ]
-le小于或等于[ 3 -le $mynum ]
-ge大于或等于[ 3 -ge $mynum ]
-z空串[ -z $myvar ]
-n非空串[ -n $myvar ]
=两个字符相等[ $myvar = one ]
!=两个字符不等[ $myvar != one ]

详细文件比较运算符

运算符描述示例
-e filename文件或目录存在[ -e /var/log/syslog ]
-d filename目录存在[ -d /tmp/mydir ]
-f filename常规文件存在[ -f /usr/bin/grep ]
-L filename符号链接存在[ -L /usr/bin/grep ]
-r filename文件可读[ -r /var/log/syslog ]
-w filename文件可写[ -w /var/mytmp.txt ]
-x filename文件可执行[ -x /usr/bin/grep ]
filename1 -nt filename2filename1 比 filename2 新[ /tmp/etc/services -nt /etc/services ]
filename1 -ot filename2filename1 比 filename2 旧[ /boot/bzImage -ot arch/i386/boot/bzImage ]

脚本示例

#!/bin/bash
# This script prints a message about your weight if you give it your
# weight in kilos and height in centimeters.
if [ ! $# == 2 ]; then
echo "Usage: $0 weight_in_kilos length_in_centimeters"
exit
fi
weight="$1"
height="$2"
idealweight=$[$height - 110]
if [ $weight -le $idealweight ] ; then
echo "You should eat a bit more fat."
else
echo "You should eat a bit more fruit."
fi

执行例子:

# weight.sh 70 150
You should eat a bit more fruit.
# weight.sh 70 150 33
Usage: ./weight.sh weight_in_kilos length_in_centimeters

位置参数 $1$2$N$# 代表了命令行的参数数量,$0 代表了脚本的名字。第一个参数代表 $1,第二个参数代表 $2,以此类推。参数数量的总数存在 $# 中。上面的例子显示了怎么改变脚本,如果参数少于或者多余2个来打印出一条消息。

-x 选项检查脚本的执行情况:

# bash -x weight.sh 60 170
+ weight=60
+ height=170
+ idealweight=60
+ '[' 60 -le 60 ']'
+ echo 'You should eat a bit more fat.'
You should eat a bit more fat.

#shell-scripting