Linux Shell Programming Learning Notes

xiaoxiao2021-03-06  70

First, the shell variable mainly has local variables and environment variables. 1, local variable - use 1 in the user's existing running script 1) Define local variable format: variable-name = value example: [Root @ jike1 / root] # localtest = "test" [root @ jike1 / root] # Echo $ localtest (Note: Echo $ localtest and Echo $ {localtest} is the same) (add $ with the variable name, you can get the value of this variable, using the Echo command to display the value of the variable) 2) Display the local variable format : Set example: [root @ chinaitlab root] # set 3) Clear local variable format: unset variable-name, for example: [root @ jike1 / root] # unset localtest At this point, Echo $ localtest will not see the variable LocalTest's output .

2, Environment Variables - Use 1 in all child processes 1) Define environment variable format: export variable-name = value (more export key than the definition of local variables): [Root @ chinaitlab / root ] # export domain = "chinaitlab.com" [root @ chinaitlab shell] # vi Testenv.sh #! / bin / bash # indicates the use of Bash to resolve the script # Testenv.sh Echo $ domain [root @ chinaitlab shell] # chmod X Testenv.sh [root @ chinaitlab shell] # ./testenv.sh chinaitlab.com 2) Display environment variable format: ENV (Display of local variables uses set, environment variable display Env): [root @ chinaitlab test] # ENV 3) Clear environment variable format: Unset variable-name is the same as local variables, use unset) examples: [Root @ chinaitLab shell] # unset domain This time is executed again ./Testenv.sh will not see the variable Domain Output. 3. Other variables 1) Location Variables $ 0, $ 1, $ 2, $ 3 ... $ 9 2) Read-only Variable Readonly variable Note: Read-only variables cannot be cleared and changed, so be cautious. 3) Special variables $ #, $?, $$ (indicating the PID of the current process) ... 2. Operators and Expressions 1, the operator is a directive to the computer, the operator type is: arithmetic operator ( , -, *, /) Bit operator (~, <<, >>, &, ^) logic operator (&&, ||,>, = =, <,! =) assignment operator (=, =, - =, * =, / =, =, & =, ^ =, | =, << =, >> = 2, the expression is an operator and an arithmetic object.

1) $ []: You can accept the digital expression ECHO $ ​​[10 1] (Output: 11) Echo "$ [2 3], $ HOME" (Output: 5, / root) Echo $ [Output: 5, / Root) Echo $ [ 2 << 3], $ [8 >> 1] (Output: 16, 4) Echo $ [2> 3], $ [3> 2] (Output: 0, 1 Expression Output 0 When FALSE, True Output 1) 2) Character expression: Direct writing, single quotes, double quotes cause. Echo "$ HOME, That is your root directory." (Output: / root, ") Echo '$ Home, That is your root directory.' (Output: $ HOME, That is your root directory.) The difference between single quotes and dual quotes is that single quotes are displayed, and the double quotes are displayed.

3) TEST expression III, control structure 1, IF statement example: #! / Bin / bash # if.sh IF [10 "-lt" 12 "] # Note: IF and [between [and" 10 " Between, "12" and] have spaces. If you do not add space, you will have syntax error. -n "ENTER A Start OR Stop:" Read Ans Case $ ANS in Start) Echo "Select Start" ;; Stop) Echo "Select Stop" ;; *) Echo "` basename $ 0`: you select is not between Start and stop "> & 2 # Note:> and & 2 There is no space,> & 2 means exit to the output to the standard output (generally on the screen) exit ;; ESAC 3, for loop statement Format: for Variable Name in List DO Command 1 Command 2 ... DONE 4, UNTIL Cycle Scripture Format: Until Condition DO Command 1 Command 2 ... DONE 5, While Cycle Schemale:

While Command DO Command 1 BREAK Command 2 Continue Command 3 ... DONE IV, Enter Output 1, Several Important Tools 1) Echo Example: Echo -n "Enter a Number from 1 To 2:" (-N Indicates that it is not changed, at which time the cursor is stopped at the end of the line) 2) READ example: read ANS (indicating the user's input to variable ANS) 3) CAT (display file content) 4) Pipe (|) (output of a program As an input to another program: LS -L | GREP "D" 5) file redirect (> and >>) example: ls -l> /tmp/a.txt (rewrite the result of the output to a. TXT This file) LS -L >> /TMP/A.txt (append the result of the output to a.txt this file, often used for log log) 6) Standard input ($ 0), standard output ($ 1) and standard Error ($ 2) is defined using a file descriptor ($ 0, $ 1, $ 2).

2, instance: readme.sh #! / Bin / bash # readname.sh echo -n "first name:" read firstname echo -n "last name:" Read lastname subname echo -e "your first name is: $ {FIRSTNAME } / n "# 加-表示 表示 将 解 解 为 转 转 字字 e": "" "字 字 字 表示 表示 字 表示" 表示 表示 表示 表示 表示 表示 y e e y 表示 表示 表示 y y e y y i y y y i i y "Your first name is: $ {firstname} / n" >> firstname.txt # This line did not add -e, so display / n echo "Your Last Name is: $ {lastname} / n" in the output Lastname.txt echo "Your Subname is: $ {subname} / n"> & 1 5, text filter 1, regular expression (like pattern matching) 2, Find (Find file) case: find ./ -Name "* .txt. "-Print 3, GREP (Finding Character) Example: GREP" [5-8] [6-9] [0-3] "Access_log 4, awk (put a series of data fields, ie, is divided into columns): awk ' {Print $ 1 "/ T" $ 4} 'Access_log 5, SED (Search and Replace Data): Sed -n' S / ChinaitLab / Hello / P 'MyFile.txt (Will M The chinaitLab in the yfile.txt file is replaced with Hello and prints to the screen, and s represents replacement, p represents printing. If you add a redirector, you can output the replaced content to a file.

转载请注明原文地址:https://www.9cbs.com/read-110305.html

New Post(0)