LINUXShell special characters

Source: Internet
Author: User
LINUXShell special character representation # 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 & quot; The # h... 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 $ {P ATH # * :}# 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 "bb" 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 ". "instance used as source #! /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. in the regular expression, the range [a-z] <> redirection and process replacement ls-al> a.txt scriptname> filename redirects the output of scriptname to the 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 '\ 'Textfile | the output of the pipeline analysis frontend command, and the output is used as the input of the following command. 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' '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.

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.