Advanced bash Script Programming Guide

Source: Internet
Author: User

Directory


Part 1. Warm up

  1. Why use shell programming?
  2. Start with a sha-bang (Sha-bang refers #!)
    1. Call a script
    2. Preliminary exercises


Part 2. Basics

  1. Special characters
  2. Introduction to variables and parameters
    1. Variable replacement
    2. Variable assignment
    3. Bash variables are of no type.
    4. Special variable types
  3. References (translation may be incorrect, especially quotation marks)
    1. Reference variable
    2. Escape (/)
  4. Exit and exit status
  5. Tests
    1. Test Structure
    2. File test operations
    3. Other comparison operations
    4. Nested if/then condition test
    5. Check your test knowledge
  6. Operators and related topics
    1. Operator
    2. Numeric constant


Part 3. Beyond Basic

  1. Variable re-game

    1. Internal Variable
    2. Operation string
    3. Parameter replacement
    4. Variable of the specified type: declare or typeset
    5. Indirect reference of Variables
    6. Random: generates random integers.
    7. Double parentheses
  2. Loop and branch
    1. Loop
    2. Nested loop
    3. Loop Control
    4. Test and branch (case and select structure)
  3. Internal commands and built-in
    1. Job control commands
  4. External filters, programs, and commands
    1. Basic commands
    2. Complex commands
    3. Time/date command
    4. Text Processing Command
    5. File and archiving commands
    6. Communication commands
    7. Terminal Control commands
    8. Mathematical computing commands
    9. Hybrid commands
  5. System and management commands
    1. Analyze a system script
  6. Command replacement
  7. Arithmetic Extension
  8. I/O redirection
    1. Use Exec
    2. Code block redirection
    3. Application
  9. Here documents
    1. Here strings
  10. Rest Time


Part 4. Advanced

  1. Regular Expression

    1. A Brief Introduction to Regular Expressions
    2. Wildcard
  2. Subshell (subshells)
  3. Restricted Shell (restricted shells)
  4. Process replacement
  5. Function
    1. Complex Functions and function complexity
    2. Local variable
    3. Recursion without local variables
  6. Alias (aliases)
  7. List Structure
  8. Array
  9. /Dev and/proc
    1. /Dev
    2. /Proc
  10. About zeros and nulls
  11. Debugging
  12. Option
  13. Gotchas
  14. Script Programming Style
    1. Unofficial shell script style
  15. Miscellaneous
    1. Interactive and non-interactive shells and scripts
    2. Shell Packaging
    3. Test and comparison: Another Method
    4. Recursion
    5. Color script
    6. Optimization
    7. Tips
    8. Security Topics
      1. Infected script
      2. Hide shell script source code
    9. Transplant topic
    10. Shell programming in Windows
  16. Bash, version 2 and 3
    1. Bash, Version 2
    2. Bash, version 3


35. Postscript

  1. Post-author
  2. About the author
  3. Where can I get help?
  4. Tools used to create this book
    1. Hardware
    2. Software and typographical Software
  5. Credits

Bash Programming

I. Bash special characters

1. wildcard:

*: Match any string

? : Match any single character

Set operators: Use a single word, a continuous range, or intermittent character set to work as wildcards.

[Set]: match a single character with a wildcard character set, such as [aeiou], [A-O], [a-h, W-Z]

[! Set]: A set wildcard consisting of all characters except the set

2. curly braces (can be nested ):

Format: [leading String] {string 1 [{nested string 1…}] [, Character transfer 2…]} [Successor string]

For example, c {A {R, t, n}, B {R, t, n} s equals to cars cats cans cbrs cbts cbns

3. Other special characters:

<: Input redirection

>;: Output redirection (create if no file exists, and overwrite if any)

>;>;: Redirection of the output. (if no value is set, it is created. If yes, It is appended to the end of the file)

(: Starting with a sub-shell, the sub-shell inherits some environment variables of the parent shell.

): The Sub-shell ends.

{: Starting from the command block, it is executed by the current shell and all environment variables are retained.

}: The command block ends.

|: MPS queue

/: Reference a single character

': Strongly referenced string. special characters are not interpreted.

": Weak referenced string, interpreting all special characters

~ : Root directory

': Command replacement

;: Command separator (command Terminator), run multiple commands in one line

#: Line comment

$: Variable expression

&: Execute commands in the background

*: String wildcard

? : Single Character wildcard

Ii. Bash Variables

1. Custom Variables

A user-defined variable consists of letters, numbers, and underscores. the first character of the variable name cannot be a number, and the variable name is case sensitive.

Varname = Value Note bash cannot leave spaces on both sides of the equal sign

Shell language is a non-type interpreted language. Assigning a value to a variable actually defines the variable and can assign different types of values. There are two ways to reference a variable: $ varname and $ {varname}. We recommend that you use the second method to prevent ambiguity of the variable in the string. The value of the referenced undefined variable is null.

Assign a value to a variable using quotation marks. Note the difference between ',', and '. ''is equivalent to $ ()

To make the variable usable in other processes, You need to export the variable: Export varname

2. Environment Variables

You can use the set command to assign values to the variables or view the environment variable values. Use the unset command to clear the variable values. Use the Export command to export the variables so that other processes can access the environment variables.

3. location variable

The location variable corresponds to the command line parameter. $0 indicates the Script Name, $1 indicates the first parameter, and so on. If the number of parameters exceeds 9, $ {} must be used to reference the variable. Shell retains these variables and does not allow users
Define them in another way. The location variables passed to the script or function are local and read-only, while other variables are global (you can declare them as local with the local keyword ).

4. other variables

$? : Save the return code of the previous command.

$-: Provides options when the shell is started or when the set command is used

$: Process ID of the Current Shell

$! : Process number of the previous sub-process

$ #: Number of parameters passed to the script or function, that is, the number of location variables minus 1, excluding the Script Name.

$ *: A single string consisting of parameters passed to the script or function, that is, the string starting from the first parameter after the Script Name is exceeded, each parameter is separated by $ ifs (generally, the internal domain separator $ IFS is 1 space ). Like "..."

$ @: List of parameters passed to the script or function. These parameters are expressed as multiple strings. It is like ""…. The difference between $ * and $ @ is convenient to use two methods to process command line parameters, but the parameter appearance is no different during printing.

For example: # vi posparm. Sh

Function cutparm

{Echo-E "inside cntparm: $ # parms: $ */N "}

Cntparm "$ *"

Cntparm "$ @"

#./Posparm. Sh abc bca cab

Inside cntparm: 1 parms: abc bca cab

Inside cntparm: 3 parms: abc bca cab

Iii. Bash Operators

1. String operators (replacement operators)

$ {Var:-word} If var exists and is not empty, return its value; otherwise, return word

$ {Var: = word} If var exists and is not empty, return its value. Otherwise, assign word to VaR and return its value.

$ {Var: + word} If var exists and is not empty, word is returned; otherwise, null is returned.

$ {Var :? Message} If var exists and is not empty, its value is returned,

Otherwise, "bash2: $ var: $ message" is displayed, and the current command or script is exited.

$ {Var: Offset []} returns a length substring of VaR starting from the offset position,

If no length exists, it is the end of the VaR string by default.

2. pattern matching Operator

Starting from the VaR header, $ {var # pattern} deletes the shortest mode string that matches pattern and returns the remaining string.

$ {Var # pattern}: Starting from the VaR header, delete the longest pattern string that matches pattern and return the remaining string. basename path =$ {path ##*/}

$ {Var % pattern}: starting from the end of VaR, delete the shortest mode string that matches pattern and return the remaining string. dirname path =$ {PATH % /*}

Starting from the end of VaR, $ {var % pattern} deletes the longest pattern string that matches pattern and returns the remaining string.

$ {Var/pattern/string} replaces the longest pattern string matched with pattern in Var with string.

4. Shell conditions and test commands

Bash can use [... ] Structure or test command to test Complex Conditions

Format: [expression] or test expression

Returns a code indicating whether the condition is true or false. If the return value is 0, the return value is true. Otherwise, the return value is false.

Note: spaces between the left and right brackets are required.

1. File test operators

-D filefile exists and is a directory

-E filefile exists

-F filefile exists and is a common file

-G filefile exists and is a SGID (set group ID) File

-R file has the read permission on the file.

-S filefile exists and is not empty

-U filefile exists and is a SUID (Set User ID) File

-W file: write permission on file

-X file has the execution permission on the file. If it is a directory, it has the search permission.

-O file has File

-G file: test whether it is a member of the file group.

-L filefile is a symbolic link.

File1-nt file2file1 is newer than file2

File1-ot file2file1 is older than file2

2. String Operators

Str1 = str2str1 and str2 match

Str1! = Str2str1 and str2 do not match

Str1 <str2str1 is smaller than str2

Str1>; str2str1 is greater than str2

-The length of N strstr is greater than 0 (not blank)

-The length of Z strstr is 0 (empty string)

3. Integer Operators

Var1-EQ var2var1 equals var2

Var1-ne var2var1 is not equal to var2

Var1-ge var2var1 greater than or equal to var2

Var1-GT var2var1 greater than var2

Var1-Le var2var1 less than or equal to var2

Var1-lt var2var1 less than var2

4. logical operators

! Expr

Expr1 & expr2 evaluate the logic and of expr1 and expr2. When expr1 is false, expr2 is not executed.

Expr1 | expr2 evaluates the logic of expr1 and expr2, and does not execute expr2 when expr1 is true

Note: The logic of another logical operator and expr1-A expr2 logic or expr1-O expr2

V. Shell Flow Control

1. Condition Statement: If

If condition ifs =:

Thenfor dir in $ path

Statement do

[Elif condition if [-O dir]; then

Statement] echo-e "/tyou own $ dir"

[Elseelse

Statement] echo-e "/tyou don't own $ dir"

Fifi

2. deterministic loop: fordone

For value in listfor docfile in/etc/*/usr/etc /*

Dodo

Statements using $ valuecp $ docfile ?=docfile=.doc=.txt

Donedone

Note: For var ;... Equivalent to for VAR in "$ @";...

3. Uncertainty loop: While and

While condition until Condition

Dodo

Statement

Donedone

Count = 1 COUNT = 1

While [-n "$ *"] Until [-z "$ *"]

Dodo

Echo "parameter $ count" Echo "parameter $ count"

Shiftshift

Count = 'expr $ count + 1' COUNT = 'expr $ count + 1'

Donedone

The condition is true. The execution cycle body condition is false.

Note: Definitions and algorithms of Integer Variables

Declare-I idx defines integer variables using $ () without definition

Idx = 1

While [$ idx! = 150]

Do

CP somefile. $ idx

Idx = $ idx + 1 integer algorithm idx = $ ($ idx + 1 ))

Done

Another algorithm, Echo $ (100/3), puts the addition, subtraction, multiplication, division expressions into $ ().

4. Select structure: Case and select

The case expression in expression is compared with the mode in sequence, and the first matching mode is executed.

Mode 1); enables the program control flow to jump to esac for execution, equivalent to break

Statement; allow the expression to match with a pattern containing wildcards

Mode 2)

Statement ;;

......

[*)

Statement]

Esac

Select value [in list] menu automatically generated by list

If do does not have a list, it is the location variable by default.

Statements using $ Value

Done

For example: IFS =: Set the domain separator:

PS3 = "choice>;" changes the default select prompt

Clear

Select dir in $ path

Do

If [$ dir]; then

CNT =$ (LS-Al $ dir | WC-l)

Echo "$ CNT Files in $ dir"

Else

Echo "no such choice !"

Fi

Echo-e "/npress enter to continue, CTRL-C to quit"

Read: Press enter to continue the program. Press Ctrl + C to exit.

Clear

Done

5. Command shift

Pass the command line parameters stored in the location variable to the left in sequence.

Shift n the command line parameter transmits n strings to the left

Vi. Shell functions

Definition: function fnamefname ()

{{

Commandscommands

}}

Call: fname [parm1 parm2 parm3...]

Description: functions are defined before use. The two definitions have the same functions.

The function name and the called function parameter become the location variable of the function.

The variables in the function should be declared as local variables using local.

VII. Input and Output

Limited space. All input and output operators and functions are not discussed here.

1. I/O redirection

<: Input redirection

>;: Output redirection (create if no file exists, and overwrite if any)

>;>;: Redirection of the output. (if no value is set, it is created. If yes, It is appended to the end of the file)

<: Enter redirection (here document)

Format: Command <label

Input...

Label

Note: Make the input of a command a shell script (input ...), Until the end of the label

For example: CAT <$ home/. profile>; out

Echo "add to file end !" >;>; $ Home/. Profile

FTP: User = Anonymous

Pass = YC@163.com

FTP-I-n <End-I: non-interactive mode-N: Disable Automatic Logon

Open ftp.163.com

User $ user $ pass

CD/pub

Close

Endend mark input end

2. String I/O operations

String output: Echo

Command Option:-E: Start escape sequence-N: Cancel output and wrap

Escape Sequence:/a: alt/Ctrl + g (Bell)/B: backspace/Ctrl + H

/C: line feed after canceling output/F: formfeed/Ctrl + J

/R: return/Ctrl + M/V: vertical Tab

/N: octal ASCII character //: Single/character/T: Tab

String input: Read

It can be used for user interaction input or for processing a line in a text file at a time.

Command Option:-A: Read the value into the array. The array subscript starts from 0.

-E: Use the GNU Readline library to read data and allow bash editing.

-P: print the prompt before reading

For example, ifs =:

Read-p "Start read from file. filename is:/C" filename

While read name pass uid gid gecos home shell <FILENAME

Do

Echo-E "Name: $ name/npass: $ pass/N"

Done

Note: If the number of fields in a row is greater than the number of variables in the variable table, all subsequent fields are appended to the final variable.

8. Command Line Processing

Command Line Processing Command: getopts

There are two parameters. The first is an option list string consisting of letters and colons, and the second is a variable name.

The option list string is composed of option letters starting with a colon. If an option requires a parameter, the option letter is followed by a colon.

Getopts splits the first parameter and extracts the option and assigns it to the second parameter variable.

If an option has parameters, read the parameters to the built-in variable optarg.

The built-in variable optind stores the value of the command line parameter (location parameter) to be processed.

After the Option List is processed, getopts returns 1; otherwise, 0 is returned.

For example, while getopts ": xy: Z:" OPT

Do

Case $ opt in

X) xopt = '-xset ';;

Y) yopt = "-yset and called with $ optarg ";;

Z) zopt = "-zset and called with $ optarg ";;

/?) Echo 'usage: getopts. Sh [-x] [-y Arg] [-Z Arg] File... '

Exit 1

Esac

Done

Shift ($ opting-1) removes the processed command line parameters

Echo $ {xopt:-'Did not use-x '}

Echo $ {yopt:-'Did not use-y '}

Echo $ {zopt:-'Did not use-Z '}

Echo "remaining command-line arguments are :"

For f in "$ @"

Do

Echo-E "/T $ F/N"

Done

9. Process and Job Control

Signal Processing Command: Trap

Format: trapcommandsig1sig2...

Trap can recognize more than 30 types of signals, such as interrupt (CTRL + C) and suspension (CTRL + Z). You can use kill-L to view the signal list.

When the script receives signals such as sig1 and sig2, trap executes the command. After the command is complete, the script is re-executed.

Signals can be identified by names or numbers.

Job control commands: BG, FG

BG: displays background processes, that is, processes that are suspended by Ctrl + Z or executed by 'COMMAND & '.

FG: transfers the background process to the foreground for execution.

Kill-9% N: Kill the nth background process

Appendix:

1. Bash-supported command line parameters

-A outputs all variables

-C "string" reads the command from string

-E: use non-interactive mode.

-F prohibit the generation of shell file names

-H Definition

-I Interactive Mode

-K is the command execution setting option.

-N: reads the command but does not execute it.

-R-restricted mode

-S command read from standard input

-T execute a command and then exit Shell

-When U is replaced, an error occurs when unspecified variables are used.

-V: Display Shell input lines

-X trace mode: displays the executed commands.

Many modes can be used in combination. You can use set to set or cancel shell options to change the shell environment. Enable option "-" And disable option "+". If the options set in shell are displayed, run: $ echo $-

2. The shell environment variables in profile are as follows:

The search path used when cdpath executes the CD command

Home user's home directory

The domain delimiter inside ifs, which is generally a space character, Tab character, or line feed.

Mail specifies the path of a specific file (Mailbox), which is used by the Unix Mail System

Path: Search Path of the command (the path of config. sys in DOS)

PS1 main command prompt, default is "$"

PS2 from the command prompt, the default is "> ;"

Term terminal type

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.