Shell_02 basic operation arithmetic operation (expr) -- only integer operations + addition-subtraction * multiplication/Division (integer) % remainder operations in bash $ (()) $ [] expr60 \ * 60 \ * 24expr60 \ * 60 \ * 7expr1 + 1expr1*1 expr $(10 + 1... shell_02 basic operation arithmetic operation (expr) -- only integer operations + addition-subtraction * multiplication/Division (integer) % remainder operations in bash $ (()) $ [] expr 60 \ * 60 \ * 24 expr 60 \ * 60 \ * 7 expr 1 + 1 expr 1*1 expr $(10 + 10) /10 expr $(10 + 10 )) /10 expr 10% 3 # expr $ RANDOM % 10 # let "v = 2 ** 16" # echo $ v65536 exit in Linux. every time the command is executed, the system returns an exit status. The exit status is expressed in an integer to determine whether the command is running correctly. If the exit status is 0, it indicates that the command runs successfully. if the exit status is not 0, the exit status of the last command failed to be executed is saved in the built-in variable "$?" So you can use the echo statement to test whether the command runs successfully. 0 indicates that the operation is successful, and no problem occurs during program execution ~ 125 indicates that the operation failed. the script command, system command error, or parameter passing error 126 found the command but could not be executed> 128 command is forced by the system to end the conditional expression: test Command purpose: test whether a specific expression is true. when the condition is set, the return value after the command is executed $? It is 0. Otherwise, it is in another numeric format: test condition expression [condition expression] test can be used to test which conditions are represented: 1. file status 2. Comparison of strings 3. Comparison of integers 4. Multi-condition combination (|| &&!) (-A-o !) ---------------------------------------------------- # Man test-a = &-o = |! Single condition :[! 1-eq 1] The results of the two values are reversed. the exclamation point priority is the lowest, unless it is enclosed in parentheses ;! [1-eq 1] Multiple conditions: [1-eq 1-a 10-eq 10] = [1-eq 1] & [10-eq 10] The two conditions are true; [1-eq 1-o 10-eq 10] = [1-eq 1] | [10-eq 10] obtain or; [1-eq 1-o 10-eq 10-o 1-eq 1] = [1-eq 1] | [10-eq 10] | [1-eq 1] ----------------------------------------------------- # test 1 = 1 # echo $? 0 #[1 = 1] -- [] = test -- single condition # echo $? 0 #[1 = 1] & echo YESYES #[1 = 2] & echo YES -- no return |-o or &-a and! Combination of conditions: [1 = 1-a 1 = 2] and combination [1 = 1] & [1 = 2] and combination [1 = 1] | [1 = 2] or combination [1 = 1-o 1 = 2] or a combination of logical operators 1) logical operators include logical non-logical operators, logical operators, and logical operators. The following table describes logical operators! Expression if expression is false, the test result is true expression1 a expression2 if both expression1 and expression are true, the test result is true expression1 o expression2 if expression1 and expression2 have one true, the test condition is true. 2) The following table lists the true and false tables of logical operators. expr1 and expr2 are expressions used to describe a test condition. Expr1 expr2! Expr1! Expr2 expr1 a expr2 expr1 o expr2 True/False false [! 1-eq 1] use-eq as much as possible without =;-eq for integer processing; = for string processing; shell for internal processing; <字符串或串(string)是由数字、字母、下划线组成的一串字符。一般记为 s="“a1a2···an”(n"> = 0 ). It is the data type that represents text in programming languages.> Common expression types 1, test file status 2, string comparison 3, integer comparison 4, logical test file expressions: -whether a file exists # [-a/etc] & echo YES -- directory YES # [-a/etc/passwd] & echo YES -- file YES # [- /etc/passwdddd] & echo YES -- no return value ----------------------------------------B file exists, it is also a device file # [-B/dev/sda] & echo YESYES # [-B/dev/passwd] & echo YES -- no return value --------------------------------c file, and the character file # [-c/dev/tty1] & echo YESYES # [-C/etc/passwd] & echo YES -- no return value ----------------------------------------d file exists, and the type is Directory # [-d/etc/] & echo YESYES # [-d/etc/passwd] & echo YES -- no return value ----------------------------------e file. whether it exists. --------------------------------------F file exists, and the regular file # [-f/etc/passwd] & echo YESYES # [-f/etc/] & echo YES -- no return value ----------------------------------g file exists, and set the force bit # find/-perm-2000 -- find which files in the system have the force bit # [-g/usr/bin/wall] & echo YESYES # [-g/ usr/bin/vim] & echo YES -- no return value ----------------------------------------h file exists, it is also a soft link file ------------------------------------k f Ile has a log Directory [-k/tmp] & echo YESYES # [-k/root] & echo YES -- no return value --------------------------------------p file to test the pipeline file # find /- type p -- find which pipeline files are in the system # [-p/tmp/vmware-root.2] & echo YESYES # [-p/etc/passwd] & echo YES -- No returned value -------------------------------------- r file exists, and the current user has the read permission # su-user01 -- switch to the common user $ [-r/etc/passwd] & echo YESYES $ [-r/etc/shadow] & echo YES -- no response The value ------------------------------------s file exists, and the file content is not blank # touch/opt/a.txt # [-s/opt/a.txt] & echo YES -- no return value # [-s/etc/passwd] & echo YESYES --------------------------------------t file descriptor (FD) it is a file opened in a terminal ----------------------------------u file with suid # find/-perm-4000 # [-u/bin/mount] & echo YESYES # [-u/etc /passwd] & echo YES -- no return value ------------------------------------- -W file Test file write permission # [-w/etc/passwd] & echo YESYES # [-w/etc/] & echo YESYES $ [-w/etc/passwd] & echo YES -- no return value ------------------------------------x file test whether the execution permission is available $ [-x/bin/ls] & echo YESYES $ [-x/sbin/unix_update] & & echo YES -- no return value ------------------------------------- the following are uppercase letters: ------------------------------------O file test whether the file owner is the current user $ touch/tmp/a $ [-O/tmp/a] & echo YES YES $ [-O/etc/passwd] & echo YES -- no return value --------------------------------------G file this file Group (main group) $ touch/tmp/a $ [-G/tmp/a] & echo YESYES $ [-G/etc/passwd] & echo YES -- no return value ------------------------------------L file True if file exists and is a symbolic link. -- the file exists and is soft link ----------------------------------S file. the test object is socket # [-S/dev/log] & echo YESYES # [-S/etc/passwd] & Echo YES -- no return value ------------------------------------N file has not been changed since the last time # [-N/etc/passwd] & echo YES -- no return value # useradd #[ -N/etc/passwd] & echo YESYES ------------------------------------- file1-nt file2 -- check if file1 is newer than file2-n (with t) true if file1 is newer (according to odification date) than file2, or if file1 exists and file2 does not. # [/etc/passwd-nt/var/log/messages] & Echo YES # [/var/log/messages-nt/etc/passwd] & echo YESYES ------------------------------------- file1-ot file2 -- test whether filt1 is older than file2 True if file1 is older file2, or if file2 exists and file1 does not. # [/var/log/messages-ot/etc/passwd] & echo YESYES ------------------------------------- file1-ef file2 True if file1 and file2 refer to the same device and inode numbers. # [/etc/I Nit. d-ef/etc/rc. d/init. d] & echo YES -- can be used to test whether the two files are hard-linked. YES --------------------------------- string expression:-z string true, if the string is null. # [-Z''] & echo YESYES # [-z 'A'] & echo YES -- no return value --------------- string-n string true, if the string is not empty. # [-N 'A'] & echo YESYES # [-N'] & echo YES -- no return value --------------- string1 = string2 True if the strings are equal. = may be used in place of = for strict POSIX compliance. # ['a' = 'a'] & echo YESYES # ['a' = 'AB'] & echo YES --------------- string1! = String2 True if the strings are not equal. # ['A '! = 'AB'] & echo YESYES # ['A '! = 'A'] & echo YES -- no return value ----------------- string1 <string2 True if string1 sorts before string2 lexicographically in the current locale. # [aa \ <aaa] & echo YESYES # [aaa \ <ccc] & echo YESYES string1> string2 True if string1 sorts after string2 lexicographically in the current locale. integer comparison expression: arg1 OP arg2-eq equal equals-ne not equal to-lt lesser than less than-le lesser equal less than or equal to-gt granter then greater -Ge granter equal is greater than or equal to # [1-eq 1] & echo YESYES # [1-eq 2] & echo YES # [1-ne 2] & echo YESYES # [1-lt 2] & echo YESYES # [2-gt 1] & echo YESYES # [1-le 1] & echo YESYES # [1-le 2] & & echo YESYES # [2-ge 2] & echo YESYES # [3-ge 2] & echo YESYES logical combination of expressions: 1. &-a logic and 2, |-o logic or 3 ,! The logic is not [1-gt 2]-a [-f/etc/passwd] [1-gt 2] & [-f/etc/passwd] or: [1-gt 2]-o [-f/etc/passwd] [1-gt 2] | [-f/etc/passwd] not: [1-gt 2]-[! -F/etc/passwd] summary: 1. determine the file type-f-O-u-L-h-nt-ot-k-s-Stest-f/etc/passwd [-f/etc/passwd] 2, string-z-n =! ==<= 3, integer-eq-gt-ge-lt-le structured statement: if for while until case function ---------------------------------------------- If statement -- used to judge a point if list; then list; [elif list; then list;]... [else list;] fi The if list is executed. if its exit status is zero, the then list is executed. otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and t He command completes. otherwise, the else list is executed, if present. the exit status is the exit sta-tus of the last command executed, or zero if no condition tested true. if single branch if []... thencommand01... fi dual-branch if [] thencommand01elsecommand01fi for -- Cyclic structured statements (generally taking values within a given range or range) format 1: for name [in word]; do list; done format 2: for (expr1; expr2; expr3); do list; done for I in 1 2 3 doecho $ idon E for I in {1 .. 10} doecho $ idone for (I = 1; I <= 10; I ++) doecho $ idone while -- loop while when the condition is true while: doecho hellodone until -- loop until [] doecho hellodone continue/break while when the condition is set -- loop while [1 = 1] doecho "I love you forever" done when the condition is set # while :; do echo 'I love you forever !!! '; Sleep 3 s; done # while true; do echo' I love you forever !!! '; Sleep 3 s; another done while mode: # vim a.txt 123abc456 while read linedoecho $ linedone cat a.txt | while read line; do echo $ line; done until -- loop until when the condition is not true [1! = 1] docommand1command2done break -- when a loop is executed, if the condition is met, the loop can be forced to jump out of the loop. at this time, the loop ends continue. when the condition is met, the loop can also jump out, but the next element can be loops. Case -- branch judgment statement (a plane (interval) can be determined) case $1 incase1) command01; case2) command02; case3) command03 ..... *) esac function 1. when writing a Shell script program, some command operations that need to be reused are defined as commonly used statement blocks, which can be called Functions 2. reasonable use of Shell functions, the script content can be more concise, the program's ease of use can be enhanced, and the execution efficiency can be improved. function_name () {command1command2} function_name -- call the function function_name () {} to display the selection menu: select name in [a B c] do done ---------------------------------------------- The Chinese character belongs to the string format # sh-n xxx. sh check syntax # sh-x xxx. sh debugging mode ----------------------------------------- • bash shell debugging method • enable debugging mode • $ set-x • $ bash-x shell. sh • disable debug mode • $ set + x sh-n test. sh -- check the syntax (structured statement syntax) sh-x test. sh -- if there are too many parameters for debugging, this error occurs: too implements arguments, you need to use xargsxargs (1)-build and execute command lines from standard input # awk-F: '{print $1}'/etc/passwd | xargs-I mkdir-p {}
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