shell 条件测试语句三种方法

1、test -f file  2、[ -f file ] 3、[[ -f file ]]

[ -f file1 -a -f file2]逻辑与[ -f file1 -o -f file2]逻辑或 [ -f file1] && [ -f file2 ] 一样的 字符串的操作最好加双引号 如-n -z == != 并且比较时等号两边要加空格

###man test

( EXPRESSION )
EXPRESSION is true

! EXPRESSION
EXPRESSION is false

EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true

EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true

-n STRING
the length of STRING is nonzero

STRING equivalent to -n STRING

-z STRING
the length of STRING is zero

STRING1 = STRING2
the strings are equal

STRING1 != STRING2
the strings are not equal

INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2

INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2

INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2

INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2

FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers

FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2

FILE1 -ot FILE2
FILE1 is older than FILE2

-b FILE
FILE exists and is block special

-c FILE
FILE exists and is character special

-d FILE
FILE exists and is a directory

-e FILE
FILE exists

-f FILE
FILE exists and is a regular file

-g FILE
FILE exists and is set-group-ID

-G FILE
FILE exists and is owned by the effective group ID

-h FILE
FILE exists and is a symbolic link (same as -L)

-k FILE
FILE exists and has its sticky bit set

-L FILE
FILE exists and is a symbolic link (same as -h)

-O FILE
FILE exists and is owned by the effective user ID

-p FILE
FILE exists and is a named pipe

-r FILE
FILE exists and read permission is granted

-s FILE
FILE exists and has a size greater than zero

-S FILE
FILE exists and is a socket

-t FD file descriptor FD is opened on a terminal

-u FILE
FILE exists and its set-user-ID bit is set

-w FILE
FILE exists and write permission is granted

-x FILE
FILE exists and execute (or search) permission is granted

Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be
escaped (e.g., by backslashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of
STRING.

时间: 2024-08-07 08:36:29

shell 条件测试语句三种方法的相关文章

shell条件测试语句实例-测试apache是否开启

终于理解了shell条件测试语句”!=“和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash # www.jquerycn.cn web=`/usr/bin/pgrep httpd` if [ -n "$web" ]; //$web返回值是否为空 then echo "httpd is running" else /etc/init.d/httpd start f

Shell条件测试语句及三种if语句

7 - Shell_条件操作测试及if语句 要使Shell脚本程序具备一定的"智能",面临的第一个问题就是如何区分不同的情况以确定执行何种操作.例如,当磁盘使用率超过95%发送警告:当备份目录不存在时能够自动创建:当源码编译程序的配置失败则不再继续安装等. Shell环境根据命令执行后的返回状态值($?)来判断是否执行成功,当返回值为0,表示成功,值为其他时,表示失败. 使用专门的测试工具-test命令,可以对特定条件进行测试,并根据返回值来判断条件是否成立(返回值0为成立) 使用te

《shell条件测试语句,字符串测试apache是否开启》

还得我想了10分钟才明白"!="和"-n"的用法区别,做个笔记捋一捋 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash web=`/usr/bin/pgrep httpd` if [ -n "$web" ];  //$web返回值是否为空 then echo "httpd is running" else /etc/init.d/httpd start fi 第二种: #!/bin/bash web=`

shell条件测试语句if

if条件语句: if条件语句分为单分支结构.双分支结构.多分支结构 ----------------------------------------------------------------------------- 单分支if语句:判断目录是否存在,若不存在则自动创建. #!/bin/bash DIR="/media/cdrom" if [ ! -e $DIR ] then mkdir -p $DIR fi 双分支if语句:判断指定主机能否ping通,根据返回结果分别给予提示过

【第四章】Shell 条件测试表达式

shell中条件测试的三种格式: 格式1: test 条件表达式格式2: [ 条件表达式 ]格式3: [[ 条件表达式 ]] 使用test: [[email protected]-131 ~]# test -f file && echo true || echo false false [[email protected]-131 ~]# touch file [[email protected]-131 ~]# test -f file && echo true || e

Shell中的条件测试语句

Shell有条件测试语句,一般用test命令或是[]命令来完成,它们是条件判断语句if~then语句的基础,特别是[]命令.下面我们讲解一些条件测试语句. 1. test命令实现条件测试 对于检测系统中某些文件是否存在,或者相关属性时,test命令很好用. 其基本语法如下: test命令还可以测试字符串: test命令还可以测试整数之间的关系: 2. []命令来实现条件测试 使用中括号[]命令来实现条件测试功能时,要特别注意.因为中括号在很多地方都有用到,比如通配符和正则表达式.而在用[]表示条

shell执行三种方法

查看当前shell echo $SHELL grep root /etc/passwd 开头: #!/bin/bash #!/bin/sh ls -l /bin/sh shell种类 cat /etc/shells /etc/init.d/nfs /etc/init.d/crond /etc/init.d/rc.d/rc.sysinit bash --version shell执行三种方法: 1,sh t.s 或者 shell t.s 2,/server/scripts/t.sh 或者 ./t.

九、while 条件循环语句、case 条件测试语句、计划任务服务程序

4.3.3 while条件循环语句 while条件循环语句是一种让脚本根据某些条件来重复执行命令的语句,它的循环结构往往在执行前并不确定最终执行的次数,完全不同于for循环语句中有目标.有范围的使用场景.while循环语句通过判断条件测试的真假来决定是否继续执行命令,若条件为真就继续执行,为假就结束循环.while语句的语法格式如图4-21所示. 图4-21  while循环语句的语法格式 接下来结合使用多分支的if条件测试语句与while条件循环语句,编写一个用来猜测数值大小的脚本Guess.

防止sql注入的三种方法

常用的避免SQL注入的三种方法 一,存储过程 在学习数据库视频的时候接触过,它是存储在数据库中的一些事先编译好的指令.在用的时候不用重新编写,直接调用就好了.所以,使用它可以大大提高程序的执行效率. 那么,如果创建一个存储程序并使用它呢?这是我们今天要解决的问题 1.创建过程 可编程性--下拉菜单--存储过程--右键--查询菜单--指定模板参数的值--新建查询--输入语句--查询菜单中的分析检查语法是否正确--执行 2.具体创建语法 在创建存储程序时,为了应对各种变换的数据,通常会涉及到带参数的