Shell judgment and Comparison

Source: Internet
Author: User

1  shell 的$! ,$?, $$,[email protected]

 

  • $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.
  • [email protected]        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指定的文件或目录存在时返回真
  • -g 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
  • -h 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
  • -k 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
  • -p 当file存在并且是命令管道时返回为真
  • -r 当由pathname指定的文件或目录存在并且可读时返回为真
  • -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 ]

本文摘自网络

+++++++++++++++++++++++++++++++++++++++++++

shell判断文件,目录是否存在或者具有权限文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d639c118b82a167.html#!/bin/sh 
myPath="/var/log/httpd/" 
myFile="/var /log/httpd/access.log" 
#这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 
if [ ! -x "$myPath" ]; then 
     mkdir "$myPath" 
fi 
#这里的-d 参数判断$myPath是否存在 
if [ ! -d "$myPath" ]; then 
     mkdir "$myPath" 
fi 
#这里的-f参数判断$myFile是否存在 
if [ ! -f "$myFile" ]; then 
     touch "$myFile" 
fi 
#其他参数还有-n,-n是判断一个变量是否是否有值 
if [ ! -n "$myVar" ]; then 
      echo "$myVar is empty" 
      exit 0 
fi 
#两个变量判断是否相等 
if [ "$var1" = "$var2" ]; then 
echo ‘$var1 eq $var2‘ 
else 
echo ‘$var1 not eq $var2‘ 
fi
注意:if 的格式,语句中的空格不能少。
========================================
shell 判断语句

流程控制 "if" 表达式 如果条件为真则执行then后面的部分: if ....; then
....
elif ....; then
....
else
....
fi
大多数情况下,可以使用测试命令来对条件进行测试。比如可以比较字符串、判断文件是否存在及是否可读等等…   通常用" [ ] "来表示条件测试。注意这里的空格很重要。要确保方括号的空格。 
[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a和$b是否相等          -r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真 
#########################################################含条件选择的shell脚本
    对于不含变量的任务简单shell脚本一般能胜任。但在执行一些决策任务时,就需要包含if/then的条件判断了。shell脚本编程支持此类运算,包括比较运算、判断文件是否存在等。基本的if条件命令选项有: -eq —比较两个参数是否相等(例如,if [ 2 –eq 5 ])
-ne —比较两个参数是否不相等
-lt —参数1是否小于参数2
-le —参数1是否小于等于参数2
-gt —参数1是否大于参数2
-ge —参数1是否大于等于参数2
-f — 检查某文件是否存在(例如,if [ -f "filename" ])
-d — 检查目录是否存在
几 乎所有的判断都可以用这些比较运算符实现。脚本中常用-f命令选项在执行某一文件之前检查它是否存在。 ################################################################## 判断文件是否存在 #!/bin/sh
today=`date -d yesterday +%y%m%d`
file="apache_$today.tar.gz"
cd /home/chenshuo/shell
if [ -f "$file" ];then
echo "OK"
else
echo "error $file" >error.log
mail -s "fail backup from test" [email protected] <error.log
fi

shell 的判断与比较

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.