1. Introduction
Shell (shell) is the code that the Linux system uses to parse the user's commands into the computer's ability to execute, which is then handed to the kernel for execution by the kernel. The types of shells supported by this Linux system can be viewed through the/etc/shells file.
A shell script is a shell script that saves some of the commands written in a certain order to a file, and then gives the file execute permission.
2. Format of shell scripts
[[email protected] ~]# VI shell.sh //Use VI Editor to write shell scripts
#!/bin/bash//Specify the type of shell that you specify
#this is a test shell scripts//Description of the script
X=1
echo $x//For the contents of the script
3. Shell scripts have the following methods of execution
1) [[email protected] ~]# sh shell.sh
1
2) [[email protected] ~]#. shell.sh
1
3) [[email protected] ~]# source shell.sh
1
4) [[email protected] ~]#./shell.sh
-bash:./shell.sh: Insufficient permissions
[Email protected] ~]# chmod a+x./shell.sh
[[email protected] ~]#./shell.sh//Direct execution need to grant execute permission to the file
1
4. Linux has the following types of interactive device files when processing data
Standard input (STDIN) file device number 0
Standard output (STDOUT) file device number 1
Standard error (STDERR) file number is 2
5. Redirection
REDIRECT: The standard data file is transferred to the specified file by redirecting the ">" symbol if the standard output data is originally displayed to the screen.
[email protected] ~]# cat./shell.sh > Cdx.txt //greater than sign for standard output
[email protected] ~]# cat Cdx.txt
#!/bin/bash
#this is a test shell scripts
X=1
Echo $x
[[email protected] ~]# ls-l/jjj 2> stderr.txt //By file number 2 The redirect symbol is imported into the specified file with the error message
[email protected] ~]# cat Stderr.txt
LS: Unable to access/JJJ: No file or directory
[Email protected] ~]# echo "abc123" > Passwd.txt
[[email protected] ~]# passwd--stdin Test < Passwd.txt //By importing the secret key into the test account via the less than sign
6. Piping symbols
You can use the value of the previous command as a parameter to the last command, and you can continue to manipulate the value.
[Email protected] ~]# ifconfig eth0 | grep "HWaddr" | awk ' {print $} '
00:0c:29:3a:a0:9a
7. Shell variables
The shell variable has the following variables: 1. Custom variable 2. Environment variable 3. Position variable 4. Predefined variables
1) Custom variables, users can directly assign values
[[email protected] ~]# a=1//assigns a value of 1 directly to a
[Email protected] ~]# echo $a
1
2) Special symbols used for variables and assignments
Double quotation mark "" When assigning a value with a space, you need to specify it as a whole by double quotation marks
[Email protected] ~]# b= "ABC def"
[Email protected] ~]# echo $b
ABC def
When a single quote is assigned a special character, it is necessary to turn the special in parentheses into ordinary characters [[email protected] ~]# [[email protected] ~]# d= ' $abc '
[Email protected] ~]# echo $d
$abc
The anti-apostrophe and "$ ()" can be executed first for the statements in parentheses
[[email protected] ~]# RPM-QF $ (which cat)
coreutils-8.4-19.el6.i686
[Email protected] ~]# RPM-QF ' which cat '
coreutils-8.4-19.el6.i686
3) Position variable
He is divided into $1,$2,$3......$9,$0 as the command variable according to the position of the parameter.
[Email protected] ~]# VI expr.sh
#!/bin/bash
#express scripts
Expr $ + $//specify $ and $
[[Email protected] ~]# SH expr.sh 12 23//Assign value to $ and $
35//Calculation result
4) Pre-defined variables
$?: View the status of the last command execution, if the value is 0 indicates that the command execution is complete. A value other than 0 indicates a problem occurred when the previous command was executed
$ A: Represents the name of the currently executing script or program.
$#: Indicates the number of positional parameters in the command line
$*: Represents the contents of all positional parameters
This article from the "Knowledge is the torch of Wisdom" blog, reproduced please contact the author!
Shell script (i)