Advanced bash Programming Guide (2)

Source: Internet
Author: User

Scope of content: Chapter 5 References

1. Echo $ (LS-l) # No line feed is output

Echo "$ (LS-l)" # Do not remove line breaks

2. Exit code

(1) Exit num (Num must be a decimal integer ranging from 0 to 25 5)

(2) If a script ends with an exit command without parameters, the exit code of the script is the exit code of the last command before the exit command is executed.

(3) No exit, no exit code and exit $? Which is equivalent

(4) When a function returns, $? The exit code of the last command in the function is saved. When a script ends, the exit code of the script is saved.

3. The logic is not (!)

! True; echo $? # Result: 1 note! Space behind

Output True after spaces are removed

4.

(1) Whether the exit code of a series of commands in an IF/then structure is 0 (0 indicates that the command is successfully executed) is different from that in C.

(2) The if command can not only test [] but also test any command.

(3) [synonym for test, built-in shell, return comparison result (0 true, 1 false)

[[] Extension, which is a keyword () Let

(4) test built-in shell. In the bash script, the test statement does not have to call external/usr/bin/test binary files. Similarly, you do not have to call/usr/bin /【; /usr/bin/[is a symbolic link to/usr/bin/test.

You can run the type command to view the information.

(5) [] structure is more common than bash [], which is an extension of the test Command introduced from ksh88.

I) All characters between [] are not extended or marked or separated by files, but there will be parameter reference and command replacement.

Ii) in [[], the test structure is better than [] to prevent many logical errors in the script, for example, &, ||,<,> the operator can pass the test in [], but an error occurs in the [] structure.

(6) () the structure expands and calculates the value of a arithmetic expression. A non-zero value expression returns a 0 or true as the exit code, which is the opposite of test, that is.

5. File test operators

(1)-G file or directory settings-ID (SGID) flag is set

If the sgid flag of a directory is set, all files created under this directory belong to the user group with the sub-directory, and the group of users who create files is not required. This feature is good for sharing directories in a working group.

(2)-u File Settings-user-ID (SUID) flag is set

If the SUID flag is set for a binary execution file owned by a root user, normal users can run the file as root. The execution of system hardware to be accessedProgram(Pppd, cdrecord) is very useful. If no SUID bit is set, these binary execution programs cannot be called by non-root users.You can use sudo.

6. Integer comparison

I)-Le,-ne,-lt, and so on are used in []. <, <= and so on are used in ().> , <Can be used in [], but escape is required .! =, =, = Can be used in []

Pay attention to the spaces on both sides of the symbol when using it.

7. String comparison

If [-n "$ string1"]: the string is true if it is not null.

If [-z "$ string2"]: the string is null and true.

! =, Other escape characters

When [[] is used, no escape is required.

8. Hybrid comparison

(1)-a logic and-O logic or

(2) If [[[condition1 & condition2] is equivalent to if ["expr1 '-A" expr2 "], if [$ condition1] & [$ condition2]

Note that comparison operators & | cannot be used in.

9. In bash, local variables in the function can be used outside the function.

10.

: $ (N = $ n + 1) a colon is required and is followed by a space. No: Bash runs the sentence as a command.

Equivalent to (n = n + 1),: $ [n = $ n + 1], let "n ++", (n ++ )),: $ (n ++),: $ [n ++]

11. Comma Operator

Link two or moreAlgorithmOperation. All operations are evaluated (side effects may occur), but the value returns the structure of the last operation.

Let "T1 = (5 + 3, 7-1, 15-4)" # T1 = 11

12. Numeric Constants

Unless a number has a special prefix or symbol, shell scripts treat it as a decimal number. A number with a prefix of 0 is octal. A number prefixed with 0x is a hexadecimal number.

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.