Shell BASICS (5) condition judgment and shell Basics

Source: Internet
Author: User

Shell BASICS (5) condition judgment and shell Basics

When writing a script, you sometimes need to judge whether the strings are equal and test the number. This lays the foundation for the shell statements, loops, and condition statements learned later.

Condition judgment format
1. test condition: test command
2. [condition]: []
3. Command Execution result: such as cd lottu.
Note: When using [], you must add spaces on both sides of the condition.

File status test

Operator Description Example
-B file Checks whether the file is a block device file. If yes, returns true. Otherwise, false is returned. [-B $ file]
-C file Checks whether the file is a character device file. If yes, returns true. Otherwise, false is returned. [-C $ file]
-D file Checks whether the file is a directory. If yes, returns true. Otherwise, false is returned. [-D $ file]
-F file Checks whether a file is a common file (neither a directory nor a device file). If yes, returns true. [-F $ file]
-R file Checks whether the file is readable. If yes, true is returned. [-R file]
-W file Checks whether the file is writable. If yes, true is returned. [-W file]
-X file Checks whether the file is executable. If yes, returns true. [-X file]
-S file Check whether the file is empty (whether the file size is greater than 0). If it is not empty, true is returned. [-S file]
-E file Checks whether a file (including directories) exists. If yes, returns true. [-E file]
The test results are as follows:
[db17 ~]$ test -f a[db17 ~]$ echo $?0[@db17 ~]$ test -f c[@db17 ~]$ echo $?1[@db17 ~]$ ls cls: c: No such file or directory[@db17 ~]$ echo $?2[@db17 ~]$ test -d lottu[@db17 ~]$ echo $?0 

Relational operators

Operator Description Example
-Eq Checks whether two numbers are equal. returns true if they are equal. [1-eq 1] returns true
-Ne Checks whether two numbers are equal. If they are not equal, true is returned. [10-eq 8] returns true.
-Gt Checks whether the number on the left is greater than the number on the right. If yes, returns true. NA
-Lt Checks whether the number on the left is smaller than the number on the right. If yes, returns true. NA
-Ge Checks whether the number on the left is equal to the value on the right. If yes, returns true. NA
-Le Checks whether the number on the left is less than or equal to the number on the right. If yes, returns true. NA
= Equal. Used to compare two numbers. If the two numbers are the same, true is returned. [$ A = $ B]
! = Not equal. Used to compare two numbers. If they are not the same, true is returned. [$! = $ B]

Note: 1. Relational operators only support numbers, not strings, unless the string value is a number.
2. Determine whether two numbers are equal. You can also use "= ","! = ". It is also used for digital purposes.

Var = 10

[db17 ~]$ [ ${var} == 5 ][db17 ~]$ echo $?1[db17 ~]$ [ ${var} -lt 5 ][db17 ~]$ echo $?1[db17 ~]$ [ ${var} -gt 5 ][db17 ~]$ echo $?0[db17 ~]$ [ ${var} -eq 5 ][db17 ~]$ echo $?1 

 

Boolean operator

Operator Description Example
! Non-operation. If the expression is true, false is returned. Otherwise, true is returned. [! False]
-O Or operation. If one expression is true, true is returned. [$ A-lt 20-o $ B-gt 100]
- Returns true only when both expressions are true. [$ A-lt 20-a $ B-gt 100]

Test

B = 5 [db17 ~] $ [! $ B = 5] [db17 ~] $ Echo $? 1 [db17 ~] $ [! -F a] # determine whether the file is a common file [db17 ~] $ Echo $? 1

 

 

String Operators

Operator Description Example
= Returns true if two strings are equal. ['A' = 'AB'] returns false.
! = Returns true if two strings are equal or not. NA
-Z Checks whether the string length is 0. returns true if it is 0. [-Z $ a]
-N Checks whether the string length is 0. If it is not 0, true is returned. [-N $ a]
Str Checks whether the string is null. If it is not null, true is returned. [$ A]
In fact, the two groups are opposite. Are they equal? Are they empty?

Note: [$ a] This is a bit interesting. Same as awk, though not as flexible as awk.

[17 ~]$ [ $b ][db17 ~]$ echo $?0[db17 ~]$ echo $b5[db17 ~]$ unset b[db17 ~]$ echo $b[db17 ~]$ [ $b ][db17 ~]$ echo $?1

 

 

Command Execution result

The execution result of a command can also be used as a condition judgment. If there are several commands, the last command can be used as a result judgment.

[db17 ~]$ lsa  b  bin  lottu[db17 ~]$ echo $?0[db17 ~]$ cd li0924-bash: cd: li0924: No such file or directory[db17 ~]$ echo $?1[db17 ~]$ ls;cd li0924a  b  bin  lottu-bash: cd: li0924: No such file or directory[db17 ~]$ echo $?1

[Note]
In this article, if... then... fi is often used in conditional statements. Study hard at that time! If you think it is helpful, just like it.


Linux shell programming if judgment statement judgment Conditions

#! /Bin/bash
A = $ (grep '> 'file | wc-l)
If [$ a = 1]; then
Statement to be executed
Fi

Shell script if judgment Condition

It makes no sense to know what this file is called .... In theory, you should use the find command or you know the location of the directory in the system.
For example
Ls | grep aaa
A = 'echo $? '
B = 'ls | grep aaa'
If [$ a-eq 0]
Then
Tar zcvf Name.tar.gz $ B
Else
Echo 'Don't have this file .'
Fi

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.