Linux shell programming: Chapter 1 basics, linuxshell
Chapter 1 Basics
Shell has the advantage of processing the underlying services of the operating system,
The advantages of Python and php are development and O & M tools, web interface management tools, and web business development.
One-click installation, optimization, and alarm Script Processing
Shell is also called the command interpreter. It can identify various commands input by users and pass them to the operating system,
In Linux, the default shell is bash.
Script Creation:
The first line starting with the script #! /Bin/bash or #! /Bin/sh
#! Also known as magic number, when executing a bash script, the kernel will The interpreter to determine which program to use to explain the content in the script.
If this parameter is not set, it is the default interpreter (bash)
The following rows are #! Comment.
Run bash test. sh
Script notes:
The content following # indicates a comment to describe the script.
Use: <BLOCK structure comment. Avoid writing each line when the comment content is long #
Redirects the code between blocks to a non-existent command to implement multiline comments.
Script Execution:
When a shell script runs in non-interactive mode (File mode), it first looks for the content in the environment variable ENV, which creates an environment file (usually. bashrc. bash_profile/etc/bashrc/etc/profile), and then execute from the environment variable file. After the ENV file is read, the shell starts to execute the content in the shell script.
During a crond task, the system environment variables are redefined in the script!
The following three methods can be used to execute shell scripts:
1. bash script-name or sh script-name
2. path/script-name or./script-name (execute the script in the current path)
3. source script-name or. script-name
Execution instructions:
The first method is the method used when the current script has no executable permission.
Second, you need to change the permission of the script file to the executable permission (chmod u + x script-name or chmod 755 script-name)
The third method is to use the source or. Dot to read or load the specified script file, and then execute all the statements in the specified shell script in sequence.
This method can take the variables and functions in the execution script to the current shell.
Similar to function call, the second line of the script should write source or. (instead of bash), then nested call can be made.
Steps:
You can write functions in vim/etc/init. d/functions.
Own (){
Echo "hhhh"
}
Then write in 3.sh
#! /Bin/sh
Source/etc/init. d/functions
Own
Then execute 3.sh (Enter command sh 3.sh) and the result is hhhh
# [] There must be spaces at both ends of the brackets [hhhhh]
Script Parameters:
$ N indicates the nth parameter passed to the script, that is, $1 indicates the first parameter.
$ # Number of command line parameters (excluding $0, that is, excluding the script name)
$0 name of the current script
$ * Parameter 1 parameter 2 ...... Returns the values of all parameters.
$ @ Parameter 1, parameter 2 ,...... Returns the values of all parameters.
$ _ Save the last number of commands executed before
In addition, if you have more than 9 parameters, you cannot use $10 to reference 10th parameters. Save the value of $1 first, and then use the shift command to delete parameter 1, $10 becomes $9, and the value of $ # is updated to reflect the remaining number of parameters.