Shell script Application (iv)

Source: Internet
Author: User
Tags ack echo command first string

    1. The return status value of the shell environment based on the command execution ($?). ) to determine if the execution succeeds, when the return value of 0 indicates success, otherwise (not 0 value) indicates a failure or an exception. Using a special test tool, the-test command allows you to test specific conditions and determine if the condition is true based on the return value (the return value of 0 indicates that the condition is true);

The following two forms are included when testing a command with test.

Test condition expression

Or

[Conditional expression]

The two approaches work exactly the same, but usually the latter form is more commonly used, and note that the brackets are enclosed in at least one space to separate them.

File test:

Common options for file testing are as follows:

-D: Test for Catalog (directory)

-E: Test whether the directory or file exists (Exist)

-F: Test for file

-R: Tests whether the current user has permission to read (read)

-W: Tests whether the current user has permission to write (write)

-X: Tests whether the current user has permission to execute (excute)

After performing a conditional test, pass the predefined variable "$?" You can get the return status value for the test command. So as to determine whether the condition is established.

Example: Do the following to test if the directory/media/cdrom exists, if the return value is $0, the presence of this directory otherwise does not exist, or although it exists but is not a directory.


[Email protected] ~]# [-d/media/cdrom]


[[email protected] ~]# echo $? //View the return value of the current command

1 //return 1 indicates the condition is not established

[Email protected] ~]# [-d/media]

[[email protected] ~]# echo $? //View the return value of the current command

0 //Return 0 indicates condition established

For a more visual view of the test results, you can combine the command output "&&" with the echo command, and when the condition is set up directly output "yes", where "&&" means "and" relationship, only if the previous command executes successfully after the subsequent command, Otherwise, subsequent commands will be ignored. Example: The above test results can be changed to:

[[Email protected] ~]# [-d/media/cdrom] && echo "yes" //No output indicates that the directory does not exist

[[Email protected] ~]# [-d/media] && echo "yes" //Output "Yes" indicates that the directory exists

Yes

2. Integers worth comparing: The integer worth comparison refers to the given two integer values, judging the relationship between the first number and the second number, such as whether it is greater than or equal to less than the second number, the common options for the comparison of integer values are as follows.

-eq: The first number equals the second number

-ne: The first number is not equal to the second number

-GT: The first number is greater than the second number

-LT: The first number is less than the second number

-le: The first number is less than or equal to the second number

-ge: The first number is greater than or equal to the second number

Example: When judging a user who has landed, when it is less than five output "Too many", you can perform the following actions.

[Email protected] ~]# unum= ' Who |wc-l '

[[Email protected] ~]# [$Unum-lt 5] && echo "Too many"

Too Many

For example, when the memory is larger than 1024MB, the following values are output:

[[email protected] ~]# freecc=$ (free-m | grep "cache:" | awk ' {print $4} ')

[Email protected] ~]# [$FreeCC-gt 1024x768] && echo ${FREECC}MB

1648MB

4. String comparison: String comparison is usually used to check the user's input, the system environment and so on whether to meet the conditions;

=: The first string is the same as the second string;

! =: The first string differs from the second string, where! means to take the reverse;

-Z: Checks whether the string is null;

Example: Determine if the current locale is en. CN;

[Email protected] ~]# echo $LANG

en_US. UTF-8

[Email protected] ~]# [$LANG! = "ZH_CN"] && echo "not en. CN "

Not en. CN

For example, in a shell scripting app, when you often enter Yes or no to confirm a task, the following actions demonstrate the simple process of confirming the interaction:

[[email protected] ~]# read-p "Do you want to overwrite the existing file (yes/no)?" Ack

Overwrite existing file (yes/no)? Yes

[[Email protected] ~]# [$ACK = "yes"] && echo "Overlay"

Coverage

[[email protected] ~]# read-p "Do you want to overwrite the existing file (yes/no)?" Ack

Overwrite existing file (yes/no)? No

[[Email protected] ~]# [$ACK = "no"] && Echo does not overwrite

Do not overwrite

4. Logic test: Refers to the determination of the dependencies between two or more conditions, when the system task depends on a number of different conditions, depending on whether these conditions are set up or one of them, etc., need a test process, common logic test operations are as follows;

&&: Logical AND, "and", the return value of the entire test command is 0 (result) only if all two conditions are true;

| |: Logical OR, means "or", as long as the specified condition is not true, the return value of the entire test is 0 (result), when tested with the Test command, | | Can be changed to "-O";

! : Logical no, means "no", the return value of the entire test command is 0 (but the result is valid) only when the condition is not true;

Example: To determine whether the current Linux system kernel version is greater than 2.4, you can do the following actions;

[Email protected] ~]# uname-r

2.6.32-431.el6.x86_64

[[email protected] ~]# mnum=$ (uname-r| awk-f. ' {print $} ')

[[email protected] ~]# snum=$ (uname-r| awk-f. ' {print $} ')

[[Email protected] ~]# [$Mnum-eq 2] && [$Snum-gt 4] && echo "Meet the Requirements"

Meet the requirements

Next time write a point if statement, I this is the foundation, slowly write a bit difficult, I am a slag, haha

This article is from the "one dish can no longer dish of the Little rookie" blog, please be sure to keep this source http://liliming.blog.51cto.com/10925034/1792977

Shell script Application (iv)

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.