Shell is a scripting language for control systems. Bash is one of the shell interpreter and the most popular one in Linux. However, there are many shell interpreters, and each type has its own characteristics, not exactly the same ..,
Common shell:
Sh, CSH, tcsh, Bash, KSh, Dash, zsh
Common bash shortcut keys:
CTRL + A: Jump to the beginning of the command line
CTRL + E: jump to the end of the command line
CTRL + u: Delete the content from the beginning of the command line to the current cursor
CTRL + K: delete all content from the current cursor to the end of the command line
CTRL + L: clear screen, equivalent to the clear command
CTRL + C: cancel or terminate
CTRL + Z: Send the current command to the background for execution
Bash command completion
You can press the "tab" key to complete the command, and press twice to display the command Completion list, which is very convenient.
Write and execute the script:
The bash script uses "#! It starts with/bin/bash, because bash script is an interpreted language, you need to call the interpreter to execute the code when the script is running ,"#! "It tells the kernel that this is a script file followed by the path of the script interpreter. The kernel will call this interpreter to execute the script.
Exercise: Write and run the first script and output a hello word!
We are used to using the vim editor, so we can use Vim. We can't use Nano. The script file should be '. sh' as the end. It indicates that it is a script .., in fact, writing a script in a common file can also be run, but normal files that can be run will generally be compiled binary programs, so it is still '. sh'. This is the default one.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/82/wKiom1RSQD_QfiU2AABYiF_aeoU553.jpg "Title =" jb.png "alt =" wkiom1rsqd_qfiu2aabyif_aeou553.jpg "/>
This is probably the case. Simply put, the '#' here indicates the meaning of the comment, and the sentence after '#' won't be executed, only '#! 'Together indicates that this is a script and will be executed by the kernel by calling the bash interpreter.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/82/wKiom1RSQbWweIGKAAA5uRrl-Bs155.jpg "Title =" zx1.png "alt =" wKiom1RSQbWweIGKAAA5uRrl-Bs155.jpg "/>
This is my script a. Sh. First, check whether there is a syntax error in the script.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/82/wKioL1RSQnSQQNP4AAAvO-baTK0368.jpg "Title =" zx2.png "alt =" wKioL1RSQnSQQNP4AAAvO-baTK0368.jpg "/>
OK. No information is displayed, indicating that there is no error in the script. You can run the script in either of the following ways:
Method 1: directly call the bash interpreter to explain the script.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/82/wKioL1RSQxvAhC7kAAA0BrNkUiI102.jpg "Title =" zx3.png "alt =" wkiol1rsqxvahc7kaaa0brnkuii102.jpg "/>
Method 2: add an executable permission to the script to execute the script directly. However, the accurate path of the script must be provided here. However, you can modify the path file, in this way, the script can be executed like a command.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/82/wKioL1RSRATASdQ7AAFn3t0u22E909.jpg "Title =" zx4.png "alt =" wkiol1rsratasdq7aafn3t0u22e909.jpg "/>
Here I have added a './' directory to execute the script directly ..
Bash variable:There are several types of variables: local variables, local variables, environment variables, special variables, and location variables.
1. Local variables: valid only for the current Shell Process and invalid for its sub-shell and other shells
Define variables: [set] var_name = "value". Set is the keyword that defines local variables. The default variables that can be omitted are local variables.
Reference variable: $ {var_name}. Braces can be omitted in most cases.
Unset var_name
Show variables: directly execute set to display all local variables
2. Local variable: this should be related to the function. Skip it here.
Define variable: Local var_name = "value"
3. environment variables:
Define variable: Export var_name = "value"
Environment variables: Export, ENV, and printenv can be used to display environment variables,
4. Location Variables
$1,..., $ n
This is the input parameter of the script. Each parameter is separated by a space. $1 indicates the first parameter of the script.
5. Special Variables
$0 script name, which is similar to the variable location.
$? Execution status of the previous command,
0 indicates success
1-255 failed ..
$ # View several parameters after a command
I don't know the next three. The teacher didn't talk about it. I'll talk about it later.
$
$!
$ *
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/83/wKioL1RSTN2SPG8kAABOMOVZEWE716.jpg "style =" float: none; "Title =" a1.png "alt =" wkiol1rstn2spg8kaabomovzewe716.jpg "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/83/wKiom1RSTITi0lvRAABU7dAFVPU423.jpg "style =" float: none; "Title =" a2.png "alt =" wkiom1rstiti0lvraabu7dafvpu423.jpg "/>
OK. I wrote a script to test the special variables and location variables. This is probably the case ..
Bash configuration file:
Bash will automatically execute some scripts in the configuration file to set some permanent attributes.
1 profile class: Provides configuration for users who log on to the interactive system. The configuration is executed only once when logging on to the system. If you run bash later, the configuration will be executed.
/Etc/profile: valid for all users. It is generally used by administrators to modify some basic information.
/Etc/profile. d/*. sh: valid for all users. The. Sh file in this folder will be executed to set software configurations.
~ /. Bash_profile: there is also a configuration file in the personal home directory, but it is hidden.
Bashrc: non-interactive Login User Configuration
/Etc/bashrc: Global
~ /. Bashrc: Personal Configuration
Notify shell to re-read the configuration file: source file, such as source. bashrc.
--------------------------------------------------------------
Configuration File Reading sequence when the system starts:
Interactive Login User:
/Etc/profile -->/etc/profile. d/*. Sh --> ~ /. Bash_profile --> ~ /. Bashrc -->/etc/bashrc
Non-interactive login users:
~ /. Bashrc -->/etc/profile. d/*. Sh
Naming requirements for variables:
Only digits, letters, and underscores can be used;
It cannot start with a number;
Keywords in the program cannot be used;
See name and meaning; totalweight
For Loop:
This is the most commonly used loop structure in shell scripts, and there are two other loop structures, while and until, respectively. But here we can say for loop.
Syntax:
For Loop:
For var_name in list; do
Statement 1
Statement 2
...
Done
The statement structure is like this. var_name indicates the variable, and in is followed by a list. The for loop traverses the list, followed by ';', this is the statement Separator in shell, followed by the DO statement, you can also start the do line, so that you can omit ';', such:
Example 1:
For I in 1 2 3 4 5 6 7 8 9
Do
Echo $ I
Done
Let's talk about this example.
In the first line, the for keyword indicates that this is a loop statement, followed by I as a variable, and in indicates the range of the variable, followed by a list, that is, the I variable must be within the range of this list,
In the second row, do begins to execute the loop body,
In the third row, done indicates that the loop ends.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CC/wKiom1RZ7oWDKxb5AABjSql2DmI857.jpg "style =" float: none; "Title =" for1.png "alt =" wkiom1rz7owdkxb5aabjsql2dmi857.jpg "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/CB/wKioL1RZ7uijxXG2AABbD6pSIOw527.jpg "style =" float: none; "Title =" for2.png "alt =" wkiol1rz7uijxxg2aabbd6psiow527.jpg "/>
OK. Next, let's talk about how to generate a list, so it is uncomfortable to manually write the list.
----------------------------------------------------
Generate a list:
1. Generate the braces {1 .. 9}, which indicates to generate the list from 1 to 9. Continue to change the last script.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/CB/wKioL1RZ8LnxOMSXAABj4cLJKfY222.jpg "style =" float: none; "Title =" for3.png "alt =" wkiol1rz8lnxomsxaabj4cljkfy222.jpg "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/CB/wKioL1RZ8LmT_V3bAABZcZ-kmuQ341.jpg "style =" float: none; "Title =" for4.png "alt =" wKioL1RZ8LmT_V3bAABZcZ-kmuQ341.jpg "/>
It seems much simpler.
2. Use the keyword seq to generate a list. The syntax format is seq 1 [step size] 9. Here we also generate 1 to 9. The step size in the middle can be omitted, step Size indicates the length of each step,
For example, seq 1 2 9 indicates 1 3 5 7 9, each time it is the last generated number plus the step size
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/CC/wKiom1RZ8a6TiEwCAABU5mDoUN8439.jpg "style =" float: none; "Title =" for5.png "alt =" wkiom1rz8a6tiewcaabu5mdoun8439.jpg "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4D/CB/wKioL1RZ8hCjWybbAABjhSYTS0c415.jpg "style =" float: none; "Title =" for6.png "alt =" wkiol1rz8hcjwybbaabjhsyts0c415.jpg "/>
SEQ has several options, but it is not very common ..
-S 'character'. This is the delimiter. The default value is '\ n', which is probably used.
Note that you need to use a single character. If you use a string, there will be another situation,
-F: This is the option that can match strings,
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4D/CB/wKioL1RZ9WKSGXsqAAC18LTdYYg850.jpg "Title =" seqq.png "alt =" wkiol1rz9wksgxsqaac18ltdyyg850.jpg "/>
This third statement can also be executed, but it separates numbers with strings, which may not be what we mean. There are still some seq usage, so I will not talk about it here ,..
3. In addition to writing a list, commands such as ls can also generate a list. You don't need to use ''to generate a list. You can understand this as mentioned earlier.
-----------------------------------------------
Write a small script, all said so much ,..
Add 100 users, from user1 to user 100, and set the user password to 111111
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4D/CC/wKiom1RZ96CCud8rAACNsevKfVk481.jpg "Title =" aaaaa.png "alt =" wkiom1rz96ccud8raacnsevkfvk481.jpg "/>
This is the case. I did not forget the passwd command. Actually, Marco leads us to practice a lot of exercises, but here I will talk about a few, there are still few exercises. It is king to practice more and practice more. There is no shortcut ....
This article from "quiet indifferent" blog, please be sure to keep this source http://vrgfff.blog.51cto.com/6682480/1572455
6. Get started with Bash, for loop, and write the simplest script