Bash Script programming
Stacking of commands
Scripting: Interpreter parsing execution
Shell: Interactive interface, programming environment
Shell: Be able to provide some internal commands, and can be through the PATH environment variable to find external commands, the command to submit to the kernel to start a process;
Programming Environment:
Process Control Statements
Sequential execution
Loop execution
Select Execute
Condition test: True, False
Elements of a procedural programming language: variables, processes, functions, arrays
Variables: Local variables, local variables, environment variables, positional parameter variables, special variables
Variable:
Numeric type: Integer, float, Boolean
Character Type: string
Bash variable is weak type; default character type;
Variable reference: ${var_name}
Quotes:
Strong quote: '
Echo ' $PATH '
Output $path
Weak reference: ""
echo "$PATH"
The specific value of the output $path
Command reference: ' Command ', $
Aa= ' echo $PATH ' or aa=$ (echo $PATH)
Assigns the command echo $PATH execution result to AA
Declare a variable to be an integer variable:
Let Var_name=value
Declare-i Var_name=value
Declare a variable to be an environment variable:
Export Var_name=value
Declare-x Var_name=value
Script writing format:
The first line: the Interpreter; #!/bin/bash
The absolute first line of the script is given Shebang starting at the first character position:
#!bin/bash
Declare the script to be executed with bash in the bin directory, not csh or anything else.
Comment lines: All lines beginning with # are comment lines, which are ignored by the interpreter
Execute script:
Give execution permission to indicate execution path;
chmod +x first.sh
Or specify permissions directly: Bash first.sh
Direct pass script to bash interpreter
Bash Script path
Export Path=...:/tmp
Modify the path variable to write the FIRST.S directory/tmp to the path
Or do not modify path, give absolute script absolute directory:/tmp/first.sh
Or give a relative path:./first.sh
Bash options:
-N: A syntax error in the test script
-X: Debug execution
Example: Bash-x test.sh
Open a child bash, then execute the command and print the execution of the command
Arithmetic operations:
$[expression]
Let Var_name=expression
$ ((EXPRESSION))
Bash command exit and exit status code
command is executed at the end of the execution in bash, it can be logged by exiting the status code
The exit status code of the script depends on the last command executed, and the custom exit status code
Exit #: #代表数字
Example: Exit 5
Run echo $ After this command exits the program and returns 5
Success: 0
Failed: 1-255
Note: Exit the script early, or you can use the Exit command to implement
Cases:
echo $?: To see whether the last command executed successfully or not, 0 indicates a successful execution
Exercise 1: Write a script
(1) Create directory/tmp/testdir;
(2) Copy the file/etc/fstab,/etc/rc.d/rc.sysinit to this directory;
#!/bin/bash
Directory=/tmp/testdir
mkdir $directory
Cp/etc/fstab/etc/rc.d/rc.sysinit $directory
Bash Script Writing Basics