Representation of special characters in LINUX Shell # Note 1. comment # comment 2. in the middle of the quotation mark and # representation # itself 3. echo $ {PATH # * :}# parameter replacement, not a comment 4. echo $(2 #101011) # Number conversion, not a comment echo "The # here does not begin a comment. "echo 'the # here does not begin a comment. 'echo The \ # here does not begin a comment. echo The # Start a comment here. echo $ {PATH # * :}# parameter replacement, not a comment. echo $(2 #101011) # Number conversion, not a comment. note that there must be spaces in the fourth place. Otherwise, comments are not recognized; separated by 1. command separation, write multiple commands in one line echo "aa"; echo "B B "2. if the if and then conditions are placed in the same row, they are also separated by; the end of the case condition 1. command separation, write multiple commands in one line echo "aa"; echo "bb" 2. if the if and then conditions are placed in the same row, they are also separated by the; echo hello; echo thereif [-x "$ filename"]; then # Note: "if" and "then" must be separated. echo "File $ filename exists. "; cp $ filename. bakelseecho "File $ filename not found. "; touch $ filenamefi; echo" File test complete. ". the command is equivalent to 1. command: prefix of the source2. file name, hide file 3. directory :. current Directory ,.. parent directory 4. regular Expression: match any single character. First, give an example ". "As s Instance used by ource #! /Bin/bash. data-file # load a data file. # It has the same effect as "source data-file", but is more portable. # The file "data-file" must exist in the current working directory because the file is referenced by 'basename. echo "variable1 = $ variable1" echo "variable3 = $ variable3" let "sum = $ variable2 + $ variable4" echo "sum = $ sum" exit 0 is the prepared include_file script, pass. data-file is introduced, which is equivalent to include data-file in C. Let's take a look at the content of data-file # This is the data file to be loaded by the script. # Such files can contain variables, functions, and so on. # In the script, you can use 'source' or '. 'COMMAND to load. # Let's initialize some variables. var Iable1 = 22variable2 = 474variable3 = 5variable4 = 97message1 = "Hello, how are you? "Message2 =" Enough for now. Goodbye. "Let's take a look at the script execution result: root @ ubuntu :~ /Resource/study/shell_study # chmod 777 include_file root @ ubuntu :~ /Resource/study/shell_study # lsclear_log data-file include_file show_selfroot @ ubuntu :~ /Resource/study/shell_study #. /include_file variable1 = 22variable3 = 5sum = 571 the above results have strongly demonstrated our desired conclusion. how to Create a hidden file: touch. data-file. the matching characters are described as follows: AB. it can represent AB + any character, handling line breaks, and must be a character AB. it cannot indicate that some references of AB "" support wildcard extension "STRING" will block (interpreted) The full reference of most special characters ''in STRING, without wildcard extension 'string', all special characters in the STRING will be blocked. this is a more intense form than the use of \ escape \ X will be "escape" character X. this is equivalent to "X" and also 'x '. \ is usually used to escape "and ', so that double quotation marks and single quotation marks will not be interpreted as special meanings. /the directory separator separates different parts of the file name (for example,/home/bozo/pr Ojects/Makefile ). it can also be used as a Division Arithmetic Operator ., multiple commands are executed, but the last comma operator is returned to link a series of arithmetic operations. although all the content is run, only the last one is returned. let "t2 = (a = 9, 15/3)" # Set "a = 9" and "t2 = 15/3" will be computed before commas, however, the output of a command can be assigned to a variable only when the 'post-referenced 'command' structure is returned. cd $ LOG_DIRif ['pwd '! = "$ LOG_DIR"] thenecho "Can't change to $ LOG_DIR" exit $ e_xdoppler here is the most powerful description. This method is not found in the previous chapter, here, the pwd command returns the current path and compares it with LOG_DIR. You can also define a variable to save the content returned by pwd, for example, path = 'pwd': Operator 1. null operation is equivalent to "NOP" (no op, a command that does nothing ). 1: 2 echo $? #02. endless loop: while:, can be considered to be the same as the built-in command of shell, and act as true. while: do operation-1 operation-2... operation-n done # Same as below: # while true # do #... # done3. in if/then, it indicates that no operation is performed, and the branch if conditionthen: # if no operation is performed, the Branch is obtained. elsetake-some-actionfi 4. set the default parameter $ {username = 'whoamam' }:: {username = 'whoamam' }#$ {username = 'whoamam'}. If ":" Is not started, an error will be given unless "username" is a command or built-in command 5. variable replacement: $ {HOSTNAME ?} $ {USER ?} $ {MAIL ?} :$ {HOSTNAME ?} $ {USER ?} $ {MAIL ?} # If one or more necessary environment variables are not set, the error message is printed. 6. when used in combination with the> (redirection operator), a file is truncated to 0 length without modifying its permissions. If the file does not exist before, it is created. for example:> data. xxx # file "data. xxx "is cleared now. and cat/dev/null> data. xxx works the same. However, this does not produce a new process, because ":" is a built-in command. when used with the> Redirection operator, it will not affect the file to be appended. if the file does not exist, it is created. 7. it may be used as a comment line, although we do not recommend this. if you use # to annotate, the error check for the remaining rows will be disabled, so you can write anything in the comment row. however, this will not happen if you use.: This is a comment that generates an error, (if [$ x-eq 3]). 8. ":" is also used in/etc/passw D and $ PATH are used as separators. bash $ echo $ PATH/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin: /usr/games * matches 0 or more characters; mathematical multiplication; ** Power Operation root @ ubuntu :~ /Resource/study/shell_study # lsclear_log data-file include_file show_selfroot @ ubuntu :~ /Resource/study/shell_study # echo * clear_log data-file include_file show_self? Match any character, but in (a> B? A: B) indicates the Three-Objective Operation in C Language (t = a <45?) # Ternary operations in the C language style. $ character 1. echo $ PATHvar1 = 5var2 = 23 skidooecho $ var1 #5 echo $ var2 #23skidoo2. the regular expression indicates the end of the row in the regular expression. "$" indicates the end of the row. Analyze the following example in the regular expression: root @ ubuntu :~ /Resource/study/shell_study # echo slfjalj $ fdjglslfjalj3. $ {} Replace $ {PAHT }#! /Bin/bash 2 # param-sub.sh 3 4 # whether a variable is declared or set, 5 # + will affect whether the variable uses the default value, 6 # + even if the variable value is null ). 7 8 username0 = 9 echo "username0 has been declared, but is set to null. "10 echo" username0 =0 {username0-'whoamam'} "username0 is defined and its initialization is null, so no output is provided here, here "-" is equivalent to "=" 11 # No output. 12 13 echo 14 15 echo username1 has not been declared. 16 echo "username1 =1 {username1-'whoamam'}" Here username1 is not defined on it and initialized to null, so 17 is displayed # The default value is output. 18 19 username2 = 20 echo "username2 has been declared, but is set to null. "21 echo" username2 =2 {username2:-'whoamam'} "Here username2 is initialized and initialized to null, but here there is a": "22 # ^ 23 # Will be output, because-will have one more conditional test. 24 # compare it with the preceding example. 25 26 27 #28 29 # Another One: 30 31 variable = 32 # The variable has been declared but is set to a null value. 33 34 echo "$ {variable-0}" # (no output) 35 echo "$ {variable: -1} "#1 36 # ^ 37 38 unset variable 39 40 echo" ${variable-2} "#2 41 echo" $ {variable:-3} "#3 42 43 exit 0 let's also look at his execution results: root @ ubuntu :~ /Resource/study/shell_study # chmod 777 para_sub root @ ubuntu :~ /Resource/study/shell_study # lsclear_log data-file include_file para_sub show_selfroot @ ubuntu :~ /Resource/study/shell_study #. /para_sub username0 has been declared, but is set to null. username0 = username1 has not been declared. username1 = rootusername2 has been declared, but is set to null. username2 = root ^ 1234. $ * All parameters 5. $ # number of parameters 6. $ ID7. $? Process return status () characters 1. command Group, run in a sub-Shell (a = 3; echo $ a). The variables defined in the group are not available in parentheses, the rest of the script is unavailable. the parent process, that is, the script itself, cannot read the variables created in the child process, that is, the variables created in the Child shell. 1 a = 123 2 (a = 321;) 3 4 echo "a = $ a" # a = 123 5 # variable a in parentheses, more like a local variable. 2. array initialization: array = (a, B, c) braces extended 1 cat {file1, file2, file3}> combined_file 2 # concatenate file1, file2, and file3, and redirects to combined_file. 3 4 5 cp file22. {txt, backup} 6 # copy "file22.txt" to "file22. B {} code block, which is an anonymous function, but the Defined variables are still available later #! /Bin/bash # Read rows from/etc/fstab. file =/etc/fstab {read line1read line2read line3} <$ File echo "First line in $ File is:" echo "$ line1" echoecho "Second line in $ File is: "echo" $ line2 "echoecho" third line in $ File is: "echo" $ line3 "exit 0 execution result: root @ ubuntu :~ /Resource/study/shell_study #. /test1 First line in/etc/fstab is: #/etc/fstab: static file system information. second line in/etc/fstab is: # third line in/etc/fstab is: # Use 'blkid-o value-s uuid' to print the universally unique identifier. Next let's look at an example :#! /Bin/bash {echo "Just for a test: "echo 'pwd' echo" Test end "}>" test-context "# redirects all the output in the code block to the file. echo "Results of rpm test in test-context" exit 0 to check the running result: root @ ubuntu :~ /Resource/study/shell_study # chmod 777 test2 root @ ubuntu :~ /Resource/study/shell_study #./test2 Results of rpm test in test-contextroot @ ubuntu :~ /Resource/study/shell_study # lsclear_log include_file show_self test2data-file para_sub test1 test-contextroot @ ubuntu :~ /Resource/study/shell_study # cat test-context Just for a test:/root/resource/study/shell_studyTest end {}\; use $ find-name * In-exec of find *. txt-exec cat {}\; [] 1. test [-z $1] 2. array Element a [1] = 'test' 3. [[] indicates that [[...] is used for testing. condition judgment structure, instead of [...], it can prevent many logic errors in the script. for example, the &, |, <, and> operator can normally exist in the [[] condition judgment structure, but if it appears in the [] structure, an error is returned. 4. () mathematical operations 5. the regular expression indicates the range [a-z] <> redirect and process replacement ls-al> a.txt scriptname> filename redirection script Name output to file filename. if filename exists, it will be overwritten. command &> filename redirects the stdout and stderr of the command to filename. command> & 2 redirect the stdout of the command to stderr. scriptname> filename: append the output of scriptname to the file filename. if filename does not exist, it will be created. [I] <> filename: open the file filename for reading and writing, and assign the file descriptor I to this file. if filename does not exist, the file will be created.> <it is also used to compare the word boundary in the if ["$ veg1" <"$ veg2"] \ <, \> regular expression with ASCII. for example, bash $ grep '\ <the \> 'textfile | the output of the Pipeline Analysis frontend command, and the output is used as the back Command input. this is a good way to generate a command chain. echo ls-l | sh # transmits the "echo ls-l" output to the shell, which is the same as a simple "ls-l" result. cat *. lst | sort | uniq # merge and sort all ". lst file, and then delete all duplicate rows. A pipeline is a typical method for inter-process communication. stdout of a process is put into stdin of another process. the standard method is to pass the output of a General Command, such as cat or echo, to a "filter command" (the input will be processed in this filter command), and then get the result. cat $ filename1 $ filename2 | grep $ search_word of course, the output command can also be passed to the script. #! /Bin/bash # uppercase. sh: Modify the input, all converted to uppercase. tr 'a-Z' # The character range must be referenced by "" to prevent single-character file names. exit 0 now Let's deliver the ls-l output to a script. bash $ ls-l |. /uppercase. sh-RW-RW-R -- 1 BOZO 109 APR 7 1. TXT-RW-RW-R -- 1 BOZO 109 APR 14 2. TXT-RW-R--R -- 1 BOZO 725 APR 20 DATA-FILE the stdout ratio of each process in the pipeline must be read by the next process as stdin. otherwise, the data stream will be blocked and the pipeline will produce some unexpected behavior. cat file1 file2 | ls-l | sort # the output from "cat file1 file2" does not appear. work It is the pipeline for sub-processes to run and cannot change script variables. variable = "initial_value" echo "new_value" | read variableecho "variable = $ variable" # variable = initial_value if an exception occurs for a command in the pipeline, the pipeline will be terminated too early. this behavior is called broken pipe, and in this state, a SIGPIPE signal will be sent.> | force redirect (even if the noclobber option is set-C option ). this will forcibly overwrite an existing file. | logic or operation; when used between two commands, it indicates that when the previous command ends, if the returned value is false, continue to execute the next command & logical and; when used between two commands, it indicates that if the return value is true at the end of the previous command, continue to execute the next command & run in the background to see an example #! /Bin/bash # background-loop.shfor I in 1 2 3 4 5 6 7 8 9 10 # first loop. do echoecho-n "$ I" done & # Run this loop in the background. # After 2nd loops, It will be executed in some cases. echo # This 'echo 'will not be displayed in some cases. for I in 11 12 13 14 15 16 17 18 19 20 # The second loop. doecho-n "$ I" done echo # This 'echo 'will not be displayed in some cases. exit 0. Check the result: root @ ubuntu :~ /Resource/study/shell_study #./for_test 11 12 13 14 15 16 17 18 19 20 root @ ubuntu :~ /Resource/study/shell_study #-if you want to use option parameters in all commands, add "-" to the front "-". 1. parameter option 2. minus 3. redirect stdin and stdout: cd/source/directory & tar cf -.) | (cd/dest/directory & tar xpvf-) 4. previous working directory cd-5. note: The file name and variable name starting with "-" may have some problems + the option mark of a command or filter. ~ Home directory ~ + Current working directory ~ -In the previous working directory ^ regular expression, $ IFS at the beginning of the line is used to separate some input commands. By default, many of the commands in the blank space are not thoroughly studied, see the specific commands later.