Shell programming
All the code is to simple basic sentence assembly, according to their own will to write, and finally get the desired functional program.
1, the beginning: #. /bin/bash
#。 Is the program that tells the system to follow the parameters used to execute the file. The following code, as input, goes into the bash environment to execute.
2, the annotation #后面的就是注释.
3, set execution permissions. chmod u+x filename
When you first created the shell file, Cheng that 644 (umask) used the above command to make the shell file executable. Enter the file name in terminal to execute.
4, the execution of several methods of the shell,
bash filename, in effect, invokes a new bash command interpreter, passing the shell program file name as a parameter to it.
Bash < filename. Similar to the previous one, input redirection is performed so that the shell's input is taken from the established program file.
When executing a shell program, it is best to do so in the order that a newly created shell is not yet correct and should be debugged using bash + filename, but once the shell is fixed then the file is set to executable using the chmod command. You can execute the shell script only if you want to enter the appropriate name later.
5, The Bash program debugging
Bash + options + program filename
-E exits when a failed command is encountered
-N reads the command but does not execute the command.
-u substitution to treat variables that are not set as errors.
-V displays them when they are read into the shell input line.
-X displays commands and their arguments when the command is executed
When you write a shell program, you can write the set-option and set + option to write code between the two options that can be debugged with this option function.
6, Bash internal command.
The bash command interpreter contains some internal commands.
echo Eval EXEC (directly executes the established command, the next source program's commands do not execute the direct terminate process.) ) Export
ReadOnly read shift wait for exit. Point
7. Position parameters in the shell.
What is particularly interesting about the shell program is that its parameters are determined in the command line that invokes the shell program according to its location. Enter positional arguments after the program name, separated by a space between the parameters. The first argument replaces an analogy. The special is $, which is the file name of the current shell program, so the $ $ is not a positional parameter and does not include the $ $ when displaying all the current positional parameters.
8, Shell environment variables.
When the shell starts executing, it has defined some variables that are relevant to the system's working environment, and the user can redefine it and adapt it to suit its own use.
Home: The full path name used to hold the user host directory.
TERM: Type of terminal.
Path: Used to save a colon-delimited pathname, the shell searches these directories according to the path order given by the path variable, and the first command that is found to have a consistent executable file is executed.
PS1: main prompt, Root is # Normal user is $,
PS2: In the process of the shell combing user input command, if the user input "\" means that the command has not been completed, the next line continues to enter, this parameter is the prompt, ">"
9, predefined variables.
A variable that was defined at the outset, except that the user can only use the variables based on the definition of the shell
The commonly used predefined variables are:
$# the number of positional arguments $* the contents of all positional parameters $? The return status of a command after execution can be followed by whether the execution of a command succeeds.
$$ the process number of the current process $! The last process number that runs in the background is the name of the currently executing process.
10. Parameter substitution variable
The shell provides the parameter substitution function to allow the user to assign different values to the variable according to the different conditions, the change of the parameter substitution has four kinds, only writes changes that usually and touches a position parameter is that relation, according to the establishment position parameter has already set the decision variable the value.
Variable =${parameter-word} If you set a parameter, replace the value of the variable with a parameter, or replace it with Word, the value of which is equal to the value of the parameter.
Variable =${parameter =word} If you set a parameter, replace the value of the variable with the argument, otherwise set the variable to Word, and then use Word to replace the value of the parameter, and the positional argument cannot be this way.
Variable =${parameter =. Word} If you set a parameter, then the value of the parameter replaces the value of bin, otherwise it will show word and exit from the shell, if you omit word, then display the standard information, the variable requirements must be equal to touch the value of a parameter, if the parameter is not set, display a message, to accommodate the beginning of the core,
Variable =${parameter +word} If you set a parameter, Word replaces it, otherwise it is not replaced.