Linuxshell --- conditional judgment command

Source: Internet
Author: User
Linuxshell --- the test function of the conditional judgment Command 1. test command is used to detect system files and their related attributes, such as checking whether a file exists and the permissions of a file. To determine whether a file jifeng exists in the current directory, run the following command: [c-sharp]... linux shell --- conditional judgment Command 1. the test command is used to test system files and their related attributes, such as checking whether a file exists and the permissions of a file. To determine whether a file jifeng exists in the current directory, run the following command: [c-sharp] test-e jifeng, but the execution result does not receive any messages, but can be passed through $? Or & | to display the entire process. For example, the subclass above is rewritten to [c-sharp] test-e jifeng & echo "exit" | echo "not exit, the final result tells us whether it is "exit" or "not exit ". 2. the following are common test marks of the test Command: (1) type check of a file name (whether the file exists or not)-e: whether the "file name" exists. (Commonly used)-d: whether the file name is a directory. (Commonly used)-f: whether the file name is a common file. (Commonly used) B, c, S, p, L respectively refer to block devices, character devices, socket files, pipeline files, and link files. (2) test-r filename-r: whether the file has the readable attribute-w: whether the file has the writable attribute-x: whether the file has the executable property-s: whether the file is non-blank,-s indicates that the file exists, and not empty (3) compare two files (test file_a nt file_ B) -nt: whether the file file_a is newer than file_ B-ot: whether the file file_a is earlier than file_ B-ef: determines whether the two files are the same file and can be used to determine the hard connection. (Mainly determine whether two files point to the same inode) (4) test n1-eq n2-eq: equal-ne: not equal-gt: The former is greater than the latter (greater than)-lt: The former is smaller than the latter (less than)-ge: the former is greater than the other-le: The former is less than or equal to the latter (5) judge the string test-z str: judge whether the string is null, if it is null, return true (commonly used) test-n str: determines whether the string is not empty. if the left path is not empty, return true (-n can also be omitted) test str_a = str_ B and test str_a! = Str_ B: judge whether the two strings are equal or not. (6) test-r file-a-w file-a: and. true is returned only when both conditions are met, that is, the file has read and write permissions-o: or. if the two conditions are met, true-! is returned -!: Condition inversion, test -! -X file: returns true only when the file does not have execution permission. 3. you can use the judgment symbol [] to judge the data. for example, to check whether a variable is empty [-z $ SHELL], note the brackets ("[]"). must be separated by spaces. There are the following scripts: [c-sharp] #! /Bin/bash read-p "input you choice: "choice [" $ choice "=" y "] | [" $ choice "=" Y "] & echo" OK, continue "& exit 0 [" $ choice "=" n "] | [" $ choice "=" N "] & echo" Oh, interrupt! "& Exit 0 echo" I don't know what is your choice "& exit 0 4. apply the reverse operation [c-sharp] [! -S "errortrade/errortrade.txt"] & [! -S "errortrade/errorkeyvalue.txt"]
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.