linux 命令 之判断表达式

摘自http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html

Tests

There is a function provided by bash called test which returns a true or false value depending on the result of the tested expression. Its syntax is:

test expression

It can also be implied as follows:

[ expression ]

The tests below are test conditions provided by the shell:

  • -b file = True if the file exists and is block special file.
  • -c file = True if the file exists and is character special file.
  • -d file = True if the file exists and is a directory.
  • -e file = True if the file exists.
  • -f file = True if the file exists and is a regular file
  • -g file = True if the file exists and the set-group-id bit is set.
  • -k file = True if the files‘ "sticky" bit is set.
  • -L file = True if the file exists and is a symbolic link.
  • -p file = True if the file exists and is a named pipe.
  • -r file = True if the file exists and is readable.
  • -s file = True if the file exists and its size is greater than zero.
  • -s file = True if the file exists and is a socket.
  • -t fd = True if the file descriptor is opened on a terminal.
  • -u file = True if the file exists and its set-user-id bit is set.
  • -w file = True if the file exists and is writable.
  • -x file = True if the file exists and is executable.
  • -O file = True if the file exists and is owned by the effective user id.
  • -G file = True if the file exists and is owned by the effective group id.
  • file1 –nt file2 = True if file1 is newer, by modification date, than file2.
  • file1 ot file2 = True if file1 is older than file2.
  • file1 ef file2 = True if file1 and file2 have the same device and inode numbers.
  • -z string = True if the length of the string is 0.
  • -n string = True if the length of the string is non-zero.
  • string1 = string2 = True if the strings are equal.
  • string1 != string2 = True if the strings are not equal.
  • !expr = True if the expr evaluates to false.
  • expr1 –a expr2 = True if both expr1 and expr2 are true.
  • expr1 –o expr2 = True is either expr1 or expr2 is true.
时间: 2024-07-30 15:02:32

linux 命令 之判断表达式的相关文章

linux 命令执行判断依据--;,&&,||

命令执行判断依据--;,&&,|| 在linux运维过程中或者日常linux系统操作过程中会同时执行多条命令,这样的话需要通过我们的多重命令方式进行处理. 通过使用';'分号执行多个命令,如例: #sync;sync;shutdown -h now 通过判断执行命令,&&/||执行情况 如例: cmd1&&cmd2: 若cmd1执行完毕且正确执行($?=0),则开始执行cmd2 若cmd1执行完毕且错误执行($?!=0),则开cmd2不执行 cmd1||cm

linux 命令 —— test 测试条件表达式

test命令是shell中用来测试条件表达式的工具,做条件判断用的. 语法 test (选项) if [ (选项) ] 文件测试 -b<文件> block:如果文件为一个块特殊文件,则为真:-c<文件> character:如果文件为一个字符特殊文件,则为真:-S<文件> socker:如果文件为一个套接字特殊文件,则为真:-p<文件> pipeline:如果文件为一个命名管道,则为真: -f<文件> file:如果文件为一个普通文件,则为真:-

Linux下的命令执行判断

参考博主''迹忆''的文章,对linux下命令判断的使用有了初步的认识,命令执行判断是使用&&和||将多条命令联系到一起,执行的时候会通过判断上一条命令的执行结果,来决定下一条命令的执行'命运'. $? $?即上一条命令执行后的回传值,如果执行正确则返回0,如果执行错误则返回一个非0的数字. # 执行正确命令[[email protected] /home/yangchaolin]# ll decompress/ total 20 -rw-------. 1 root root 18311

linux 命令总结(转载)

linux 命令总结(转载) 1. 永久更改ip ifconfig eth0 新ip 然后编辑/etc/sysconfig/network-scripts/ifcfg-eth0,修改ip 2.从Linux上远程显示Windows桌面 安装rdesktop包 3. 手动添加默认网关 以root用户, 执行: route add default gw 网关的IP 想更改网关 vi /etc/sysconfig/network-scripts/ifcfg-eth0 更改GATEWAY  /etc/in

Linux命令自己总结

对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面我们就开始了解一下linux目录结构的相关知识. 当 在使用Linux的时候,如果您通过ls –l / 就会发现,在/下包涵很多的目录,比如etc.usr.var.bin ... ... 等目录,而 在这些目录中,我们进去看看,发现也有很多的目录或文件.文件系统在Linux下看上去就象树形结构,所以

【持续更新中】Linux命令行与Shell脚本编程大全(第3版)读书笔记12-20章

<Linux命令行与Shell脚本编程大全(第3版)>读书笔记 第十二章 使用结构化命令 根据条件使脚本跳过某些命令,这样的命令称为结构化命令(structured command).结构化命令允许改变程序执行的顺序. If-then语句: If command Then Commands Fi 如果if后的command执行退出码是0(也就是执行成功了),then后面的语句就会被执行. 也可以写成: If command; then Commands Fi 注意了,if后的command结果

基础linux命令总结

切换目录 cd ~ 回家目录 cd - 回刚才的目录 cd .. 回上级目录 cd 回家目录 ------ 显示当前所在目录 pwd 显示实际路径而非连接路径 pwd -P ------ 建立新目录 mkdir 目录名 赋予权限 mkdir -m 777 test 递归建立 mkdir -p test/test1/test2 ------ 删除空目录 rmdir 目录名 递归删除空目录 rmdir -p test/test1/test2 ------ 查看目录和文件 ls -t时间排序,-S大小

Linux命令之bc - 浮点计算器、进制转换

用途说明 Bash内置了对整数四则运算的支持,但是并不支持浮点运算,而bc命令可以很方便的进行浮点运算,当然整数运算也不再话下.手册页上说bc是An arbitrary precision calculator language,即一个任意精度的计算语言,注意是一种语言,它提供了一些语法结构,比如条件判断.循环等,可以说是很强大的,但是我在实际中还没有找 到需要这个用途的场合 .另外一个用途就是用来进行进制转换. 常用参数 一般情况下,我们使用不带任何参数的bc命令. bc 如果需要bc不输出提

Linux命令行基础 、 基础命令操作 、 目录文件基本操作

  Linux命令行基础 基础命令使用 目录和文件基本管理 #################################################   一.Linux命令行基础   1. 什么是命令.命令行    命令:能够被Linux系统识别,用来完成某一类功能的指令或程序                           |--> 依赖于Shell解释器,查看:cat/etc/shells 默认为 /bin/bash    命令行:用户输入的命令及相关参数,按Enter键提交的