shell的比较和判断

比较和判断是程序流程控制的要素,此文总结一下shell的比较和判断语句,方便查找使用。

1. 重要的比较操作符

大于: -gt

小于: -lt

大于或等于: -ge

小于或等于: -le

等于: -eq

不等于: -ne

举例:

if [ $var -eq 0]  # 如果$var等于0时

if [ $var -ne 0 -a $var -gt 2 ] #逻辑与 -a

if [ $var -ne 0 -o $var -gt 2 ] #逻辑或 -o

2. 文件相关的判断测试

[ -f $file_var ] : 如果$file_var是存在的文件路径或文件名,则返回真;

[ -x $var ] : 如果$var具有文件可执行权限,则返回真;

[ -d $var ] : 如果$var是目录,则返回真;

[ -e $var ] : 如果$var是存在的文件,则返回真;

[ -c $var ] : 如果$var是存在的字符设备文件,则返回真;

附上英文:

-a file exists. 
-b file exists and is a block special file. 
-c file exists and is a character special file. 
-d file exists and is a directory. 
-e file exists (just the same as -a). 
-f file exists and is a regular file. 
-g file exists and has its setgid(2) bit set. 
-G file exists and has the same group ID as this process. 
-k file exists and has its sticky bit set. 
-L file exists and is a symbolic link. 
-n string length is not zero. 
-o Named option is set on. 
-O file exists and is owned by the user ID of this process. 
-p file exists and is a first in, first out (FIFO) special file or named pipe. 
-r file exists and is readable by the current process. 
-s file exists and has a size greater than zero. 
-S file exists and is a socket. 
-t file descriptor number fildes is open and associated with a terminal device. 
-u file exists and has its setuid(2) bit set. 
-w file exists and is writable by the current process. 
-x file exists and is executable by the current process. 
-z string length is zero.

举例:

fpath="etc/passwd"

if [ -e $fpath ]; then

echo "file exists";

else

echo "not exist";

fi

3. 字符串比较

[[ $str1 = $str2 ]] : 当str1等于str2时,返回真;

[[ $str1 == $str2 ]] : 这是判断字符串是否相等的另一种写法;

[[ $str1 != $str2 ]] : 当str1不等于str2时,返回真;

[[ $str1 > $str2 ]] : 当str1大于str2时,返回真;

[[ $str1 < $str2 ]] : 当str1小于str2时,返回真;

[[ -z $str1 ]] : 当str1包含的是空字符串,返回真;

[[ -n $str1 ]] : 当str1包含的是非空字符串,返回真;

时间: 2024-08-10 17:18:28

shell的比较和判断的相关文章

Shell脚本IF条件判断和判断条件总结

转自:http://m.jb51.net/article/56553.htm 这篇文章主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结,需要的朋友可以参考下 前言:      无论什么编程语言都离不开条件判断.SHELL也不例外. if list then           do something here       elif list then           do another thing here       e

Shell中的条件判断语句if~then~fi

Shell中的条件判断语句是前面一篇"Shell中的条件测试语句"的升级篇,也就是说,前面的测试语句是为了现在的判断语句if~then~fi语句服务的. 我们还是按照注意点和代码实现的方式铺开: 1)基本的if-then-fi语句可以用来判断基本的单层的分支结构,其形式如下: 其中if后面的测试语句一般都使用[]命令来做.如下面的例子: #-----------------------------/chapter4/ex4-18.sh------------------ #! /bin

linux shell中的条件判断

1. 退出状态 在Linux系统中,每当一条命令执行完成后,系统都会返回一个退出状态,这个状态被存放在$? 这个变量中,是一个整数值,我们可以根据这个值来判断命令运行的结果是否正确. 通常情况下,退出状态值为0,表示执行成功,不为0的时候表示执行失败. POSIX规定的退出状态和退出状态的含义: 0       运行成功 1-255   运行失败,脚本命令.系统命令错误或参数传递错误 126     找到了该命令但无法执行 127     未找到要运行的命令 128     命令被系统强行结束

Linux学习总结(五十八)shell脚本2-逻辑判断if

1 三种基本if语句 格式1:if 条件 ; then 语句; fi #!/bin/bash a=5 if [ $a -gt 2 ]; then echo $a fi 执行结果为 5格式2:if 条件; then 语句; else 语句; fi #!/bin/bash a=5 if [ $a -gt 10 ];then echo $a else echo 0 fi 执行结果为 0格式3:if -; then - ;elif -; then -; else -; fi #!/bin/bash a=

Bourne Shell中的条件判断

条件判断是一个程序获得智能的基础,而Bourne Shell脚本则通过命令 [ 来模拟大多数编程语言中的条件表达式. shell中支持的控制结构有: (1) if then else fi (2) for in do done (3) while do done 第二种主要用于遍历,可能不需要条件判断,其它两种则免不了和 [ 命令共同使用了.下面讲解这个命令如何模拟条件表达式. 文件/目录判断[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真.[ -c FILE ] 如果 FI

Shell学习笔记 - 条件判断式

1. 判断格式 1) test 参数 文件 例: test -e /root/install.log 2) [ 参数 文件 ]  -- 推荐使用 例: [ -e /root/install.log ] 注意:中括号后面和前面需要有空格 2. 判断文件类型参数 1)-d 文件:判断该文件是否存在,并且是否为目录文件 2)-e 文件:判断文件是否存在 3)-f 文件:判断文件是否存在,并且是否为普通文件 4)-s 文件:判断文件是否存在,并且是否为非空 5)其他文件类型判断: -b 块设备文件:-c

shell 编程if条件判断与if 真假判断

if条件判断与if真假判断 目录: 1.正确写法 2.错误写法 3.总结 一.正确写法 在编写shell脚本时,为简化代码的行号及结构的简约型,通常将命令执行结果和判断通过一条语句进行编写(在C语言编写程序时,经常遇到此种写法),如: [[email protected] ~]#touch test.sh if  useradd root &>/dev/null ; then    #如果用户添加成功,则不显示,否则显示用户添加失败     echo "user1 created

linux shell 之if-------用if做判断

一 简介 str1 = str2 当两个串有相同内容.长度时为真  str1 != str2 当串str1和str2不等时为真  -n str1 当串的长度大于0时为真(串非空)  -z str1 当串的长度为0时为真(空串)  str1    当串str1为非空时为真 [ "2006.01.23" \> "2005.03.01" ] && echo dayu || echo budayu int1 -eq int2 两数相等为真  int1

shell编程之测试,判断,循环

测试,判断,循环 条件测试:判断某需求是否满足,需要由测试机制来实现; 如何编写测试表达式以实现所需的测试;        (1)执行命令,并利用命令状态返回值来判断;$?:上一个命令的返回值0:成功1-255:失败    grep "^&" /etc/init.d/functions &> /dev/null    echo $?  返回0表示有空白行,测试成功;(2)测试表达式;测试方法一:    test expression        test 2&g