Linux Test conditional Expression tool

Source: Internet
Author: User

A summary of the command that encountered test at work. (very often) work too busy to write documents too sloppy please forgive. I hope to be of some help to you.


This article is only a summary of common work used in more. (At least I've used it)


Anyway


The best way is to go to the man test to understand. [Where the Mark * * * * highlights the bottom there are examples to illustrate]


NAME

Test-check file types and compare values

Options

-b< file;: True if the file is a block special file;

-c< file;: True if the file is a character special file;

-d< file;: True if the file is a directory;

-e< file;: True if the file exists;

-f< file;: True if the file is a normal file;

-g< file;: True if the sgid bit of the file is set;

-g< file;: True if the file exists and is owned by the group;

-k< file;: True if the file's sticky bit is set;

-o< file;: True if the file exists and is owned by the user;

-p< file;: True if the file is a named pipe;

-r< file;: True if the file is readable;

-s< file;: True if the length of the file is not zero;

-s< file;: True if the file is a special file for a socket;

-u< file;: True if the suid bit of the file is set;

-w< file;: True if the file is writable;

-x< file;: True if the file is executable.


Judging expressions


if test #表达式为真

if test! #表达式为假

Test expression 1–a Expression 2 #两个表达式都为真

Test expression 1–o Expression 2 #两个表达式有一个为真

Test expression 1! Expression 2 #条件求反


Judging integers


Test integer 1-eq integer 2 #整数相等

Test integer 1-ge integer 2 #整数1大于等于整数2

Test integer 1-GT integer 2 #整数1大于整数2

Test integer 1-le integer 2 #整数1小于等于整数2

Test integer 1-lt integer 2 #整数1小于整数2

Test integer 1-ne integer 2 #整数1不等于整数2


Interpreting files


Test File1–ef File2 Two files are the same file and can be used for hard connections. The main decision is whether two files point to the same inode.

Test File1–nt File2 Determine if file 1 is newer than file 2

Test File1–ot File2 Judge the file 1 than whether the file 2 old

Test–b file #文件是否块设备文件

Test–c File #文件并且是字符设备文件

Test–d File #文件并且是目录

Test–e File #文件是否存在 (Common)

Test–f File #文件是否为正规文件 (Common)

Test–g File #文件是否是设置了组id

Test–g File #文件属于的有效组ID

Test–h File #文件是否是一个符号链接 (same-L)

Test–k File #文件是否设置了Sticky bit bit

Test–b File #文件存在并且是块设备文件

Test–l File #文件是否是一个符号链接 (Same-h)

Test–o File #文件的属于有效用户ID

Test–p File #文件是一个命名管道

Test–r File #文件是否可读

Test–s File #文件是否是非空白文件

Test–t FD #文件描述符是在一个终端打开的

Test–u File #文件存在并且设置了它的set-user-id bit

Test–w File #文件是否存在并可写

Test–x File #文件属否存在并可执行


Grammar


Format 1:test < Test expressions >

Format 2: [< test expression;]

Format 3: [[< test expression;]]


Where: Format 1 and Format 2 are equivalent. Recommended use Format 2, of course, you can choose your own. Hobby issues.

Tips:

Wildcard characters can be used for pattern matching in [[]]. The &&, | |, >, < operators can be applied to [[]], but not in [].


Syntax Examples:

[[email protected] ~]# test-f linux-1.ht &&echo 0 | | echo 1 1[[email protected] ~]# touch Linux-1.ht[[email protected] ~]# test-f linux-1.ht &&echo 0 | | Echo 10test $? -eq 0 && Echo 1| | Echo 0test! -F Linux! [Email protected] ~]# arg= "Oldboy" [[email protected] ~]# echo $argoldboy [[email protected]l ~]# test-z ' $arg ' && echo true| | Echo False False

Format 2:[< test expression;]

[[Email protected] ~]# [-F Linux] && echo true | | echo Falsefalse[[email protected] ~]# touch Linux[[email protected] ~]# [-F Linux] && echo true | | echo Falsetrue[[email protected] ~]# [-F Linux] && echo truetrue
[[Email protected]]# [$a-gt $b]&& echo 1 | | echo 0 0[[email protected]]# [$a-lt $b]&& echo 1 | | echo 01[[email protected]]# [[$a > $b]]&& echo 1 | | echo 0 0[[email protected]]# [[$a < $b]]&& echo 1 | | echo 01


Operation > and < need to be escaped in single []

[[Email protected]]# [$a < $b]&& echo 1 | | echo 0 1[[email protected]]# [$a > $b]&& echo 1 | | echo 01[[email protected]]# [$a \> $b]&& echo 1 | | echo 00[[email protected]]# [$a \< $b]&& echo 1 | | Echo 0 1



This article is from the "I am a Little bird" blog, please make sure to keep this source http://liyanchao.blog.51cto.com/9954601/1684483

Linux Test conditional Expression tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.