欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

if test 有大用 有大大用 有大大用

1. 关于某个文档的『文档类型』判断,如 test -e filename 表示存在否

-e 该『文档』是否存在(常用)

-f 该『文档』是否存在且为档案(file)(常用)

-d 该『文档』是否存在且为目录(directory)(常用)

-b 该『文档』是否存在且为一个 block device 装置

-c 该『文档』是否存在且为一个 character device 装置

-S 该『文档』是否存在且为一个 Socket 档案

-p 该『文档』是否存在且为一个 FIFO (pipe) 档案

-L 该『文档』是否存在且为一个连结档


2. 关于文档的权限检测,如 test -r filename 表示可读否 (但 root 权限常有例外)

-r 检测该文档是否存在且具有『可读』的权限

-w 检测该文档是否存在且具有『可写』的权限

-x 检测该文档是否存在且具有『可执行』的权限

-u 检测该文档名是否存在且具有『 SUID』的属性

-g 检测该文档名是否存在且具有『 SGID』的属性

-k 检测该文档名是否存在且具有『 Sticky bit』的属性

-s 检测该文档是否存在且为『非空白档案』


3. 两个档案之间的比较,如: test file1 -nt file2

-nt (newer than)判断 file1 是否比 file2 新

-ot (older than)判断 file1 是否比 file2 旧

-ef 判断 file1 和 file2 是否为同一档案,可用在判断 hard link 的判断上。 主要是在判断,两个档案是否均指向同一个 inode


4. 关于两个整数之间的比较,例如 test n1 -eq n2

-eq 两数值相等 (equal)

-ne 两数值不等 (not equal)

-gt n1 大于 n2 (greater than)

-lt n1 小于 n2 (less than)

-ge n1 大于等于 n2 (greater than or equal)

-le n1 小于等于 n2 (less than or equal)


5. 字符串判断

test -z string 判断字符串是否为空,为空返回true

test -n string 判断字符串是不为空,为空返回false




6. 多重条件判断,例如: test -r filename -a -x filename

-a (and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 不 x 权限时,才回传 true。

-o (or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 戒 x 权限时,就可回传 true。

! 反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true

————————————————


原文链接:https://blog.csdn.net/u012906135/article/details/51919626

来自 https://blog.csdn.net/u012906135/article/details/51919626



普通分类: