if语句有多种写法

[root@shell-yankerp sh]# [ -f file ] && echo "yes" || echo "no" 
yes
修改如下:

if [ -f file ] 
then
echo "yes"
else
echo "no"
fi

简写:

[ -f file ] && echo "yes" 
表示if条件成立时返回yes
[ -f file ]  || echo "no" 
表示if条件不成立时,返回no ​​​​

可以用if判断如下条件:

实际上if的条件就是test命令的参数

[omcr@lnltedmr3 ~]$ man test
TEST(1)                                                   User Commands                                                  TEST(1)

NAME
       test - check file types and compare values

SYNOPSIS
       test EXPRESSION
       test

[ EXPRESSION ]
       [ ]
       [ OPTION

DESCRIPTION
       Exit with the status determined by EXPRESSION.

--help display this help and exit

--version
              output version information and exit

An omitted EXPRESSION defaults to false.  Otherwise, EXPRESSION is true or false and sets exit status.  It is one of:

( 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.

NOTE: [ honors the --help and --version options, but test does not.  test treats each of those as  it  treats  any  other
       nonempty STRING.

NOTE:  your shell may have its own version of test and/or [, which usually supersedes the version described here.  Please
       refer to your shell's documentation for details about the options it supports.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report test translation bugs to  <http://translation‐
       project.org/team/>

AUTHOR
       Written by Kevin Braunsdorf and Matthew Bradburn.

COPYRIGHT
       Copyright    ©    2013   Free   Software   Foundation,   Inc.    License   GPLv3+:   GNU   GPL   version   3   or   later
       <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       The full documentation for test is maintained as a Texinfo manual.  If the info and test programs are properly  installed
       at your site, the command

info coreutils 'test invocation'

should give you access to the complete manual.

GNU coreutils 8.22                                          June 2018                                                    TEST(1)

shell脚本if语句的多种条件参数的更多相关文章

  1. shell脚本if语句后面的中括号[]与java的if后面的小括号不同(),实际上[左中括号相当于test命令

    四.shell 中的条件判断命令 test 和 [   test 命令可以处理 shell 脚本中的各类工作.它产生的不是一般的输出,而是可使用的退出状态.test 命令通过接受各种不同的参数,来控制 ...

  2. shell脚本编程(一) 变量、条件判断、循环

    目录   1. shell脚本编程   2. 运行 Shell 脚本有两种方法   3. 变量   4. 本地变量   5. 环境变量   6. 参数变量   7. 多行注释   8. if条件判断 ...

  3. kettle文件自动化部署(shell脚本执行):命令行参数传入

    shell脚本中调用kitchen 和 pan去执行,job和transformation文件.分 windows和 dos系统两种. 举个简单的小例子 shell脚本: export JAVA_HO ...

  4. Shell脚本case语句

    case语句格式 case 变量 in PAT1) 执行语句 ;; PAT2) 执行语句 ;; *) 默认执行语句 ;; esac 使用示例: 编写一个shell脚本,通过提示用户输入信息,输出cpu ...

  5. 通过Shell脚本读取properties文件中的参数时遇到\r换行符的问题

    今天在编写微服务程序启动脚本的时候,遇到一个比较奇葩的问题,下面给出具体描述: 目标:通过读取maven插件打包时生成的pom.properties文件,获取里面的应用名称和应用版本号,然后拼接得到s ...

  6. Shell 脚本中 '$' 符号的多种用法

    通常情况下,在工作中用的最多的有如下几项: $0:Shell 的命令本身 $1 到 $9:表示 Shell 的第几个参数 $? :显示最后命令的执行情况 $#:传递到脚本的参数个数 $$:脚本运行的当 ...

  7. Linux Shell脚本编程-语句控制

    过程式编程语言bash脚本编程面向过程的编程  顺序执行:默认法则,按照顺序一条一条语句执行  选择执行:分支,条件判断,符合条件的分支予以执行  循环执行:将同一段代码反复执行有限次,所以循环必须有 ...

  8. linux shell脚本常用语句

    linux shell 指令 诸如-d, -f, -e之类的判断表达式: 文件比较运算符-e filename  如果 filename存在,则为真  [ -e /var/log/syslog ]-d ...

  9. shell脚本判断语句和循环语句

    if判断语句 exit跳出判读语句 不加exit的结果 read -n(不换行) 判断是否输入的是数字 read age[[ $age =~ ^[0-9]+$ ]]if [ $? -ne 0 ]; t ...

随机推荐

  1. EF优化之启动预热

    为什么Entity Framework的初始化速度慢如蜗牛呢? 对于在应用程序中定义的每个DbContext类型,在首次使用时,Entity Framework都会根据数据库中的信息在内存生成一个映射 ...

  2. ios 给微信开发一个插件并安装到未越狱的手机上教程

    现来整体说一下思路,首先给越狱的手机开发一个插件并安装上去,然后去越狱手机上找到相应的动态库和对应的微信APP安装包,拷贝出来,然后重新签名,就可以安装到未越狱的手机上了 1.首先你的电脑需要安装th ...

  3. P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈

    P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且 ...

  4. Vue使用watch监听一个对象中的属性

    问题描述 Vue提供了一个watch方法可以让使用者去监听某些data内的数据变动,触发相应的方法,比如 queryData: { name: '', creator: '', selectedSta ...

  5. windows下创建MySQL定时备份与删除脚本

    今天在windows服务器上面写了一个MySQL定时任务,备份呢与删除 rem *****************************Code start********************* ...

  6. linux服务器文件授权命令

    分两部分改属主和属组权限:更改权限,递归方式 chmod -R 755 /var/www/html/test.com更改属主,递归chown -R apache:apache /var/www/htm ...

  7. 《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐

    <Ray Tracing in One Weekend> 优点: 相对简单易懂 渲染效果相当好 代码简短,只看书上的代码就可以写出完整的程序,而且Github上的代码是将基类与之类写在一起 ...

  8. BZOJ.5092.[Lydsy1711月赛]分割序列(高维前缀和)

    题目链接 \(Description\) \(Solution\) 首先处理\(a_i\)的前缀异或和\(s_i\).那么在对于序列\(a_1,...,a_n\),在\(i\)位置处分开的价值为:\( ...

  9. LOJ.6160.[美团CodeM初赛 RoundA]二分图染色(容斥 组合)

    题目链接 \(Description\) 求在\(2n\)个点的完全二分图(两边各有\(n\)个点)上确定两组匹配,使得两个匹配没有交集的方案数. \(n\leq10^7\). \(Solution\ ...

  10. office图标(word,powerpoint,excel)异常(变成白板)问题修复

    都是wps搞的鬼. 如果先装了wps后卸载wps再装office就可能出现这个问题. 终于解决了!!先装个wps 2016,进入设置→高级→兼职性关联.这时你会发现ppt文件图标不是白板了.重启打开w ...