Shell scripting is a very important "program" in Linux.
What shell script?
A shell script is an executable statement that is designed ahead of time to complete a particular task's file.
Shell scripts can be divided into interactive and non-interactive
Interactive: Refers to the script at run time, need some artificial participation, for example, keyboard input data, confirm the option and so on. Because there is an artificial participation, so the runtime is more intelligent, but at the same time the efficiency will be reduced
Non-interactive: refers to the script run without human intervention, batch execution, high efficiency, but improve the difficulty of intelligent
2 canonical format for shell scripts
# ! Environmental Statement
# Comment Text
Executable code
The above is the canonical format of the shell script, which means that you can write the first environment statement as soon as you write whatever script you are writing. For example, one of the most famous programs in programming is ' Hello World ', a program that seems to be the first to write no matter what language you learn.
[[email protected] desktop] #vim/root/helloworld.sh
#!/bin/bash
echo "Hello world!"
So we're ready to write our first shell script.
In shell scripts, we usually end up with. SH, because you can see that it's a shell script at a glance, but it's not the end of the. SH, you can add nothing after the name.
But the shell script does not write well can run, in Linux or to involve read and write execution permissions, the general shell script after writing, the system is the default is a basic file, we need to give it executable permissions, we can run the script, so:
[[email protected] desktop] #chmod +x/root/helloworld.sh
[[Email protected] Desktop]#/root/helloworld.sh
Hello World!!
So a complete and running script is written.
3 Basic creation process for shell scripts
① Clear Mission requirements: What to do first, what to do after
② Writing code files: the implementation of each step, and then converted to a command line in the form of saving in the script
③ Test and Refine: Troubleshoot errors based on running results, code optimization, user-friendly handling
4 Simple Scripting Tips
Pipeline Delivery
Use | Pipeline operation
– The standard output from the previous command is given to the latter command to process
For example:
#!/bin/bash
Useradd Student
Echo 123 | passwd--stdin Student
This script is automatically created by the user student, and the password is set to 123. The purpose of the pipeline here is to give 123 to passwd, which is used to set the password
REDIRECT Output
Collect only the correct output from the previous command
2>: Collect only the error output from the previous command
&>: Collect errors and correct output from the previous command
For example:
[[email protected] desktop] #cat/opt/1.txt/etc/>/opt/2.txt
Cat:/etc/: is a directory
[[email protected] desktop] #cat/opt/2.txt
123
[[email protected] desktop] #cat/opt/1.txt/etc/2>/opt/2.txt
123
[[email protected] desktop] #cat/opt/2.txt
Cat:/etc/: is a directory
[[email protected] desktop] #cat/opt/1.txt/etc/&>/opt/2.txt
[[email protected] desktop] #cat/opt/2.txt
123
Cat:/etc/: is a directory
Variable
In the actual scripting process, often involves a lot of data, but these data are the same type, for example, a class of exam results, a lot of usernames and so on, if each of them to write a script is cumbersome, and is a repetitive operation, it is necessary a "container" can be stored in this data, And as the data changes, so does its content.
What is a variable?
A variable simply means storing a value that may change with the same name.
Basic form: Variable name = variable Value
Function: It is convenient to reuse a certain value with fixed name, improve the adaptability to the task requirement and the running environment transformation.
Issues to be aware of when defining variables:
– If the specified variable name already exists, it is equivalent to re-assigning a value to this variable
– Don't have spaces on both sides of the equals sign
– Variable names are made up of letters/numbers/underscores, case-sensitive
– Variable names cannot start with a number, do not use keywords and special characters
viewing and referencing variables
– Reference variable Value: $ variable Name
– View variable values: Echo $ variable name, echo${variable name}
Classification of variables (operation and maintenance angle):
Environment variables: Used to set up user/system environment
Positional variables: command-line arguments (non-interactive pass-through values) that are provided when the script is executed
Predefined variables: The execution information used to save the script, the system built-in, I call directly, cannot change
Custom variables: User-owned settings, arbitrary changes, use
Here we introduce the position variable and the predefined variable first.
Positional variables:
First, we need to know what the positional variables mean.
In Linux, variables cannot be named by pure numbers, because numbers are used to define positional variables.
For example, I have written a/root/ps.sh script that reads:
#!/bin/bash
echo $
Echo
echo $
The results of the operation are as follows:
[[Email protected] Desktop]#/root/ps.sh 1 2 3
1
2
3
As you can see, I've entered three numbers behind the script: three-in-one
The output is
So positional variables mean that each position is a variable after the script, the first position is the variable 1, the second position is the variable 2, and so on.
Note: Each location is separated by a space, otherwise the long character is a position
So the above script is to display the position variable on the screen the content of the
Pre-defined variables:
$# the number of position variables that have been loaded
$* values for all positional variables
$? Status value after program exit, 0 indicates normal, other value is abnormal
For example, the following script:
#!/bin/bash
echo $
Echo
echo $
Echo $#
Echo $*
The results of the operation are as follows:
[[Email protected] Desktop]#/root/ps.sh 1 2 3
1
2
3
3
1 2 3
As you can see, the variable $ #的作用是统计已使用的位置变量的个数, and the role of $* is to output all the used position variable contents to the screen
Variable $? The function is special, if a program output results, no error, the system will use "0" value corresponding to this result, indicating that the program is no exception, if the program error or other conditions, it will use other non-0 value to represent the result. And the value is stored in the variable $?. Normally we can't see this variable, we can show it in the form of echo $.
For example:
[[email protected] desktop] #cat/mnt
Cat:/mnt: is a directory
[[email protected] desktop] #echo $?
1
The cat command is used to view the contents of the file, when used to view the table of contents will be error, then, with the echo $? You can see the value of this variable $? is 1. This variable is the output state of the last program that is saved, and the values generated by the previous program run are overwritten.
When writing scripts, there are often options for comparing and judging, and here are some command options for these:
Check file status
-e: Document exists as true
-D: Document exists and is directory-True
-F: Document exists and is file-true
-r: Document exists with Read permission true
-W: Document exists with Write permission true
-X: Document exists with Execute permission true
Compare integer size (with E equals two words, g means greater than, L is less than)
-GT: Greater than------------------>>greater than;
-ge: Greater than or equal------------->>greater than or equal to
-eq: Equals---------------->>equal;
-ne: Not equal to--------------->>not equal
-lt: Less than--------------------->>less than
-le: Less than or equal-------------->>less than or equal to
string alignment
= =: Equal
! =: Not Equal
Shell script writing involves a lot of knowledge, here first introduce some basic simple knowledge and application.
Simple introduction and simple application of Shell Script Foundation