I. Getting Started with bash programming
Programming Languages:
Compiled language: Compiler c,c++
Interpreted language: Interpreter
Interpreter can run independently
Variables: Keep the data carrier, the command of the memory space
Local variables:
Environment variables
Local variables:
Positional parameter variable: $1,$2 、、、,
Special variables:
The name of the current script
Shell script: Shebang
#!/bin/bash # explains the shell path of this script, the kernel calls the corresponding interpreter to interpret the script
#Description:
#Version:
#Author:
#License:
#Datetime:
Script file, other lines that begin with # are comment lines that will be ignored by the interpreter
Bash script: The kernel launches a dedicated shell process to run the program files, and the script runs overnight, and this shell process terminates
To run the script:
1, the script execution permissions, and then the location of the abridged path to run
The kernel will run this script by reading the first line of the script file to determine what interpreter to start
2. Display the given interpreter to run
The bash command can check for script syntax errors:
-N Note: not a command syntax error
-X debug Run show Run process
Second, cyclic implementation
loop control statements in bash:for,while,until
for Loop:
Grammar:
For variable name in list;
Loop body
Done
Operating features:
First pass: Assigns the first element in the list the variable name definition, then executes the completion loop body;
Second time: 、、、 until the end of the element traversal, the loop exits
How the list is generated:
1, directly listed as: stu100 stu101 stu102 or {stu100,stu101,stu102}
2. Generate a list of integers such as: {start_num. End_num} Example: {1.. 10} specific format, representing 1 to 10
Seq Last
Seq First Last
Seq First STEP Last
Note: SEQ is a command to use a command to reference $ ()
3. Survival with Command execution results
1, add 10 users: stu100-stu109, password with the user name
[[email protected] script]# cat useradd.sh #!/ bin/bashfor username in {stu130,stu131,stu132};d ouseradd $usernameecho "$username" | passwd --stdin $usernamedone [[email protected] script]# bash useradd.sh changing password for user stu130.passwd: all authentication tokens Updated successfully. changing password for user stu131.passwd: all authentication tokens Updated successfully. changing password for user stu132.passwd: all authentication tokens Updated successfully.
[[email protected] script]# cat 2useradd.sh #!/bin/bashfor i in {1..10 };d ouseradd user$iecho "user$i" |passwd --stdin user$idone[[email protected] Script]# [[email protected] script]# bash 2useradd.sh changing password for user user1.passwd: all authentication tokens updated successfully . changing password for user user2.passwd: all authentication tokens Updated successfully. changing password for user user3.passwd: all authentication tokens Updated successfully. changing password for user user4.passwd: all authentication tokens Updated successfully. changing password for user user5.passwd: all authentication tokens Updated successfully. Changing password for&nbsP;user user6.passwd: all authentication tokens updated successfully. changing password for user user7.passwd: all authentication tokens Updated successfully. changing password for user user8.passwd: all authentication tokens Updated successfully. changing password for user user9.passwd: all authentication tokens Updated successfully. changing password for user user10.passwd: all authentication tokens Updated successfully. [[email protected] script]#
Getting Started with Linux bash programming