Shell condition Judgment If instance detailed

Source: Internet
Author: User
Tags comparison logical operators

Shell commands, which can be split by semicolons, or by line breaks. If you want to write multiple commands in one line, you can pass the ";" Segmentation.

Such as:

bldr:deyuy/bin/my_shell>>a=5;if [[A-GT 4]]; then echo ' OK '; fi;

If statement structure [IF/THEN/ELIF/ELSE/FI]

If condition test statement

Then

Action

[Elif Conditions

Action

Else

Action

]

Fi

About condition Testing

1) Logical operators

Logical Volume Label Express meaning
1. The detection Logical volume label for files and directories.
-F Common. Detect if "Archive" exists eg:if [-f filename]
-D Common. Detects if the directory exists
-B Detects if it is a "block file"
-C Detects if it is a "character file"
-S Detects if it is a "socket tag file"
-L Detects if it is a "symbolic Link's profile"
-E Detects if "something" exists.
2. About the logical volume label of the program.
-G Detect whether the program owned by the GID
-O Detection is owned by the program executed by the UID
-P Detects if a name pipe or FIFO is being sent between programs (to be honest, this is not a good idea.) )
3. About the property detection of the file.
-R Detect if a property is readable
-W Detects if a property can be written
-X Detect whether the property is executable
-S Detects if it is a "non-blank file"
-U Detect if a property with "SUID" is
-G Detect if a property with "SGID" is
-K Detect if a property with "sticky bit" is
4. Judgments and comparisons between two files; for example [Test file1-nt file2]
-nt The first file is newer than the second one.
-ot The first file is older than the second one.
-ef The first file is the same file as the second file (link and other files)
5. Logical AND (and) "or (OR)"
&& The meaning of the logical and
|| The meaning of a logical OR

operation symbol for meaning
= equals applies To: integer or string comparison   if in [], only characters String
! = is not equal to: integer or string comparison   if in [], only String
< less than applied: integer comparison   in [], cannot use the representation string
; greater than applied: integer comparison   in [], cannot use   to represent string
-eq equals to: integer comparison
-ne is not equal to: integer comparison
-lt less than applied: integer comparison
-gt greater than applied: integer comparison
-le less than or equal to: integer comparison
-ge greater than or equal to: integer comparison
-A both establish (and) logical Expressions –a logical Expressions
-O single-sided (or) logical expression –o logical expression
-Z empty string
-N non-empty string

2) Logical Expression Test command

  

How to use: Test EXPRESSION

deyuy/bin/my_shell>>test 1 = 1 && echo ' OK '
OK

deyuy/bin/my_shell > > Test 1 < 2 && echo "OK" | | echo "Err"     Note: Test 1 < 2 This usage is wrong
ksh:2: Cannot open
ERR 
deyuy/bin/my_shell >1> Test 1-lt 2 &am p;& echo "OK" | | echo "Err" 
OK
deyuy/bin/my_shell >> [1 < 2] && echo "OK" | | echo "ERR"    
Ksh:2: Cannot open
ERR
Deyuy/bin/my_shell >> [[1 < 2]] && echo "OK" | | echo "ERR"
OK

deyuy/bin/my_she ll >> if test 1 = 1; Then echo ' OK '; else echo ' no '; Fi
OK
deyuy/bin/my_shell >> if test 1 = 2; then echo ' OK '; else echo ' no '; fi
No
Note: All characters are separated from the logical operators directly with "spaces" and cannot be joined together.

Command1 && Command2 | | Command3
The order in which these three commands are executed is to execute command2 if the Command1 execution succeeds, or Command3 if unsuccessful.
Detailed reference: http://vbird.dic.ksu.edu.tw/linux_basic/0320bash_5.php


Thin expressions
[] Expression
Deyuy/bin/my_shell >2> [1-eq 1] && echo ' OK
Related Article

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.