Linux learning path: Linux Shell learning notes

Source: Internet
Author: User

Linux learning path: Linux Shell learning Notes 1. bash regards [[$ a-lt $ B] as a separate element and returns an exit code. Exit code 0 is true, non-zero is false for example: a = 1b = c [[$ a-lt $ B] echo $? #0 a less than B is true [[$ B-lt $ a] echo $? #1 B is less than a is false 2. (...) and let... results can also return an exit code. When the result of the arithmetic expression they test is not 0, their exit code will return non-0. Exit code 0 is true, non-zero is false for example: let "1 <2" echo $? #0 (0 & 1) echo $? #13. The if command can be used to test any command. It is not just a condition in parentheses, such as creating a script test. sh ,#! /Bin/basvectergrep-q root $1 # If the file provided by the parameter contains a root string, the returned File contains at last on occurence of root # Where-q is used to block the content obtained by the echo output grep then echo "$1 contains at last on occurence of root" else echo "$ 1 does not contain "fiexit 0 and chmod 777 test. sh, run :. /test. after h/etc/passwd, the returned File contains at last on occurence of root4. An if/then structure can contain multi-level comparison and tests (nesting) if [condition-true] then command 1 command 2... else # optional command 3 command 4... fi when if and then are in the same row of a conditional test, you must use ";" to terminate the if expression (because: if and then are both keywords). For example: if [-x "$ filename"]; then5.elif usage: elif is the form of else if reduction: if [condition1] then command 1 command 2 command 3 elif [condition 2] # same as else ifthen command 4 command 5 else default-command (type... # ls, test, cd can view the type of the corresponding command or the path under/sbin/AND/bin/) 6. several Equivalent commands: test,/usr/bin/test, [],/usr/bin/[] #! /Bin/bashechoif test-z "$1" # if/usr/bin/test-z "$1" equivalent then echo "input length is 0" else echo "input length is not 0 "fiechoif [-z" $1 "] # if/usr/bin/[-z" $1 "] equivalent then echo" input length is 0 "else echo" input length is not 0 "fiexit 07. the [[] structure does not support file extension or word separation, but parameter extension and command replacement may occur. after if, you can also use test/[] For example #! /Bin/bashdir = $ 1if cd "$ dir" 2>/dev/null #2>/dev/null hide the error message then echo "Now in $ dir" else echo" can't change to $ dir "fi9.test or, it is not necessary to have if for example #! /Bin/bashvar1 = 20var2 = 22 ["$ var1"-ne "$ var2"] & echo "$ var1 is not equal to $ var2" home =/home [-d $ home] | echo "$ home directory does not exist" Note: &: if the previous operation fails, the next operation will not be executed |: the previous operation is successful, and the last operation will not be executed 10. use the () structure to calculate and test the result of the arithmetic expression. The exit code is opposite to []. true returns 0, false returns 1 (0 )) # return 1 (1) # Return 0 (5> 4) # Return 0 (5> 9) # return 1 (5-5 )) # returns 1 (5/4) # greater than 1, returns 0 (1/2) # less than 1, returns 1 (1/0) # Returns Error, returns 111. file test operation: true is returned. If the-e file exists-a file exists (has been discarded)-f is a file under test Regular file (normal file, non-directory or device) -s file length is not 0-d tested object is directory-B tested object is block device-c tested object is character device-p tested object is pipeline-h tested File is a symbolic connection-L the file under test is a symbolic connection-S (uppercase) the file to be tested is the file descriptor that a socket-t is associated with a terminal device. The stdin [-t0] or [-t1] used to check the script is a terminal-r file with read permission, and the user-w file running the script has write permission, the user-x file that runs the script has the execution permission. the user-u set-user-id (suid) of the script is marked to the file, that is, the root permission file that common users can use, through chmod + s file implementation-k set the paste bit-O the user who runs the script is the file owner-G file's group-id and the user who runs the script is the same-N from the last object is now, whether the f1-nt f2 file f1 is modified, and whether the f1-ot f2 file f1 is earlier than the f2 f1-ef f2 file f1 and f2 are hard connected to the same file 12. binary comparison operator, comparison variable or comparison INTEGER: -eq is equal to if ["$ a"-eq "$ B"]-ne is not equal to if ["$ a"-ne "$ B"]-gt is greater than if ["$ a "-gt" $ B "]-ge is greater Equal to if ["$ a"-ge "$ B"]-lt less than if ["$ a"-lt "$ B"]-le less than or equal to if ["$" -le "$ B"] <less than (double parentheses required) ("$ a" <"$ B") <= less than or equal (...) ("$ a" <= "$ B")> greater (...) ("$ a"> "$ B")> = equal to or greater (...) ("$ a"> = "$ B") string comparison: = equals to if ["$ a" = "$ B"] = equivalent to =! = Not equal to if ["$ a" = "$ B"] <less than, in the order of ASCII letters: if [["$ a" <"$ B"] if ["$ a" \ <"$ B"] # <escape> greater than-z string is null, that is, the length is 0-n and the string is not null, that is, the length is not 0. Note: When Using-z or-n to judge the string variable, you must use "" to cause the variable. For example, if [-n $ string1] # string1 is not initialized then echo "String \" string1 \ "is not null. "else echo" String \ "string1 \" is null "fi # The result shows that string1 is not empty, error if [-n "$ string1"] # string1 is still not initialized then echo "String \" string1 \ "is not null" else echo "String \" string1 \ "is null" fi # The result shows that string1 is empty, correct results if [$ string1] # string1 nude detection then echo "String \" string1 \ "is not null" else echo "String \" string1 \ "is null" fi # correct results # However, this vulnerability exists, for example: String1 = "1> 2" if [$ string1] then echo "String \" string1 \ "is not null" else echo "String \" string1 \ "is null" fi # actually the content in [] is extended to ["1> 2"], therefore, an error occurs. When [[$ string1] is used, error 13 can be avoided. hybrid comparison:-a logic and: exp1-a exp2. If both exp1 and exp2 are true, this expression returns trueif [exp1-a exp2] with the same function as [[condition1 & condition2]-o logic or: exp1-o exp2. If exp1 or exp2, if one of them is true, the expression returns trueif [exp1-o exp2] with the same function as [[condition1 | condition2. nested if/then condition test can be used to perform nested condition test. The final result is the same as the above use & hybrid comparison if [condition1] then if [condition2] then do-something fifi

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.