加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Shell判断和比较

发布时间:2020-12-15 16:55:00 所属栏目:安全 来源:网络整理
导读:1 shell 的$!,$?,$$,$@$n $1 the first parameter,$2 the second...$# The number of command-line parameters.$0 The name of current program.$? Last command or function's return value.$$ The program's PID.$! Last program's PID.$@ Save all the pa
1  shell 的$!,$?,$$,$@

$n        $1 the first parameter,$2 the second...
$#        The number of command-line parameters.
$0        The name of current program.
$?        Last command or function's return value.
$$        The program's PID.
$!        Last program's PID.
$@        Save all the parameters.

almost any shell book will talk about them,from which you can get their detail usages.

2    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指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。 -o 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。 UNIX Shell 里面比较字符写法:

-eq 等于 -ne 不等于 -gt 大于 -lt 小于 -le 小于等于 -ge 大于等于 -z 空串 = 两个字符相等 != 两个字符不等 -n 非空串 总结:

文档比较运算符  
-e filename 假如 filename存在,则为真 [ -e /var/log/syslog ] -d filename 假如 filename为目录,则为真 [ -d /tmp/mydir ] -f filename 假如 filename为常规文档,则为真 [ -f /usr/bin/grep ] -L filename 假如 filename为符号链接,则为真 [ -L /usr/bin/grep ] -r filename 假如 filename可读,则为真 [ -r /var/log/syslog ] -w filename 假如 filename可写,则为真 [ -w /var/mytmp.txt ] -x filename 假如 filename可执行,则为真 [ -L /usr/bin/grep ] filename1-nt filename2  假如 filename1比 filename2新,则为真  [ /tmp/install/etc/services -nt /etc/services ] 
filename1-ot filename2  假如 filename1比 filename2旧,则为真  [ /boot/bzImage -ot arch/i386/boot/bzImage ] 
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法)  
-z string 假如 string长度为零,则为真 [ -z "$myvar" ] -n string 假如 string长度非零,则为真 [ -n "$myvar" ] string1= string2  假如 string1和 string2相同,则为真  [ "$myvar" = "one two three" ] 
string1!= string2  假如 string1和 string2不同,则为真  [ "$myvar" != "one two three" ] 
算术比较运算符  
num1-eq num2  等于 [ 3 -eq $mynum ] 
num1-ne num2  不等于 [ 3 -ne $mynum ] 
num1-lt num2  小于 [ 3 -lt $mynum ] 
num1-le num2  小于或等于 [ 3 -le $mynum ] 
num1-gt num2  大于 [ 3 -gt $mynum ] 
num1-ge num2  大于或等于 [ 3 -ge $mynum ]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读