Shell programming specification and variables


In some complex Linux maintenance work, a large number of repetitive input and interactive operations are not only time-consuming and laborious, but also prone to errors. Writing an appropriate Shell script program can batch process and automatically complete a series of maintenance tasks, which greatly reduces the burden of managers.

1, Shell Scripting specification

1. General

Shell script is a program file that saves the commands to be executed into a text in order and gives the file executable permissions to facilitate one-time execution. It is mainly convenient for administrators to set or manage. It can be combined with various shell control statements to complete more complex operations

2. Application scenarios

  • Shell scripts are often used for repetitive operations, batch transaction processing, automatic operation and maintenance, service operation status monitoring, scheduled task execution, etc.

  • Like the website publishing script, when we log in to the website every day, we will find that the content of each page is not invariable. Under normal circumstances, the website will regularly update the website content according to the code developed by the developers, which is called the website publishing new versions regularly.

  • However, for some websites with short update interval, manual command release is a repetitive operation, which is a waste of time and troublesome. To solve this problem, you can develop an automatic release script, which can release the script with one click efficiently, accurately and easily.

3. Shell function

  • Shell is a command interpreter. It is at the outermost layer of the operating system. It is responsible for directly talking with users, interpreting users' input to the operating system, processing various operating system output results, and outputting them to the screen for feedback to users.

  • This dialogue can be interactive or non interactive. The commands we input are not recognized by the computer. At this time, a program is needed to help us translate them into binary programs that can be recognized by the computer, and return the results generated by the computer to us.

  • There are many kinds of common Shell interpreter programs. When using different shells, there will be some differences in their internal instructions and command line prompt.

  • You can learn the types of Shell scripts supported by the current system through the / etc/shells file, as follows

[root@localhost ~]#cat /etc/shells 
/bin/sh                 #Is a soft link to the bash command (replaced by / bin/bash)
/bin/bash               #Shell developed under the framework of GNU
/usr/bin/sh             #Replaced by bash
/bin/csh                #Replaced by / bin/ bash

4. Composition of shell script

Simply put, as long as you save the various Linux commands you usually use in order to a text file, and then add executable permissions, the file becomes a Shell script.

  • For example, you can create the first script file by doing the following:
[root@localhost ~]#vim

#!/bin/bash                     #Special script declaration, indicating that the following code statements are executed through the / bin/bash program
cd /boot
ls -lh vml*
  • Add executable permissions as follows
[root@localhost ~]#chmod +x
  • Execute script file
[root@localhost ~]# ./       #The method of absolute path and relative path must have x permission
[root@localhost ~]#sh       #sh script file path
[root@localhost ~]#source   #The source script file path can also be executed through "." without x permission
[root@localhost ~]#sh <
[root@localhost ~]#cat |sh (bash)

5. How to improve the script structure

  • A qualified Shell script program should follow the standard script structure

  • It should be able to output friendly prompt information and be easier for people to understand

  • For scripts with many codes and complex structures, necessary comments should be added so that they can be easily understood by themselves and others

For example, the output information of the following improved script is easier to read

[root@localhost ~]#vim

#this is my first Shell-Script.
cd /boot
echo "The current directory is located at :"
echo "Among them vml The first files include :"
ls -lh vml*

[root@localhost ~]#./
 The current directory is located at :
 Among them vml The first files include :
-rwxr-xr-x. 1 root root 5.7M 8 November 8:54 vmlinuz-0-rescue-821a021ce0074800a769d52441ec6aca
-rwxr-xr-x. 1 root root 5.7M 8 March 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

6. Redirection and pipeline operation

Due to the particularity of "batch processing" of Shell script, most of its operation processes are located in the background without user intervention. Therefore, it is very important to learn how to extract and filter execution information.

6.1 redirection operation

Linux system uses files to describe various hardware, devices and other resources, such as hard disk, partition, CD and other device files. The user processes information through the operating system, including the following types of interactive device files.

  • Standard input:
    The default device is the keyboard. The device file is / dev/stdin and the file number is 0. The command will read the input data required during execution from the standard input file.

  • Standard output:
    The default device is the monitor. The device file is / devlstdout and the file number is 1. The command sends the output result after execution to the standard output file.

  • Standard error:
    The default device is the monitor. The device file is / dev/stderr and the file number is 2. The command sends various error messages during execution to the standard error file.

By default, the three device files use the keyboard and display as associated devices to interact with the operating system to complete the most basic input and output operations. Receiving various command strings and auxiliary control information input by the user from the keyboard, and outputting the command results to the screen; If there is an error in command execution, the error information will also be fed back to the screen.

6.1.1 redirect input

  • Redirecting input refers to changing the way of receiving input in the command from the defau lt keyboard to the specified file instead of waiting for input from the keyboard( (use the "<" operator)

  • By redirecting input, some interactive operations can be completed by reading files. For example, when using the passwd command to set a password for a user, it is very troublesome to enter the password twice according to the prompt. If you use redirect input instead, you can omit the interactive process and automatically complete the password setting:

[root@localhost /home]#vim pass.txt

123456                                                      #Enter the password string content "123456"
wq Save and exit

[root@localhost /home]#passwd --stdin gulei < pass.txt      #Get the password from the pass.txt file
 Change user gulei Your password.
passwd: All authentication tokens have been successfully updated.                          #Password changed successfully

Without interactive operation statements, it is more convenient to use in Shell script programs, which can greatly reduce the process of program interruption and improve the efficiency of script execution.

6.1.2 redirect output

  • Redirection output means that the normal output result of the command is saved in the specified file, rather than directly displayed on the screen of the display.

  • The redirection output uses the ">" or "> >" operator symbol to overwrite or append files, respectively

  • If the target file for redirection output does not exist, the file will be created, and then the output result of the previous command will be saved to the file; If the target file already exists, the output result is overwritten or appended to the file.

For example, save the CPU type information of the current host in the kernel.txt file instead of directly displaying it on the screen. The operation is as follows

[root@localhost /home]#uname -p > kernel.txt
[root@localhost /home]#cat kernel.txt           

When the original content of the target file needs to be retained, the "> >" symbol should be used to add content rather than overwrite it all. For example, append the kernel version information to the above kernel.txt file, as follows:

[root@localhost /home]#uname -r >>kernel.txt 
[root@localhost /home]#cat kernel.txt 

6.1.3 error redirection

  • Error redirection refers to saving the error information (such as option or parameter errors) during the execution of a command to a specified file instead of directly displaying it on the screen.

  • Error redirection uses the "2 >" operator, where "2" refers to the label of the error file

  • When using standard output and standard input redirection, the numbers of 1 and 0 are actually omitted

  • In practical application, error redirection is used to collect the error information of program execution, which provides a basis for troubleshooting

  • For Shell scripts, you can also redirect irrelevant error information to the empty file / dev/null, which can keep the script output concise

[root@localhost /home]#echo abc > /dev/null           #Enter an abc
[root@localhost /home]#cat /dev/null 
[root@localhost /home]#                               #There's nothing in it
  • When using the "2 >" operator, the contents of the target file will be overwritten as if using ">". If you want to append input, you should change to the "2 > >" operator

  • When the output result of the command may include both standard output information and error output information, you can use the operator ">" 2 > "to save the two types of output information to different files, or you can use the" & > "operator to save the two types of output information to the same file

[root@localhost /home]#ls /etc/passwd xxx &>> 1.txt     #Separate what is displayed incorrectly from what is displayed correctly
[root@localhost /home]#cat 1.txt
ls: cannot access xxx: There is no such file or directory

6.2 pipeline operation

  • Pipeline operation provides a mechanism for collaborative work between different commands. The output result of the command on the left side of the pipeline symbol "|" will be used as the input (processing object) of the command on the right. Multiple pipelines can be used in the same command line.

  • In Shell script applications, pipeline operations are usually used to filter the key information required

Case ①: when the grep command is used to query the user name using "/ bin/bash" as the Shell name, the qualified whole line will be output. On this basis, it can be further filtered by combining the pipeline operation and awk command. Only the user name and login Shell column are output. The operations are as follows:

[root@localhost ~]#grep "/bin/bash$" /etc/passwd          
root:x:0:0:root:/root:/bin/bash                        #Before extraction
[root@localhost ~]#grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}'
root /bin/bash
gulei /bin/bash                                        #After extraction

(the awk command is separated by a colon ":" to output the strings of the first and seventh regions, and the - F part is used to specify the separator)

Case ②: to extract the disk utilization information of the root partition, you can perform the following operations:

[root@localhost ~]#df -h                                             #Before extraction
 file system                 Capacity used available used% Mount point
/dev/mapper/centos-root   56G  8.1G   48G   15% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G  9.0M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                497M  172M  326M   35% /boot
tmpfs                    378M     0  378M    0% /run/user/0
tmpfs                    378M   12K  378M    1% /run/user/42
[root@localhost ~]#df -Th |grep "/$"| awk '{print $6}'              #"/ $" indicates a line ending in /
15%                                                                 #After extraction

2, Shell script variable parsing

1. Function of shell variable

  • Shell variables are used to store specific parameters (values) that the system and users need to use, and these parameters can be changed according to user settings or changes in the system environment

  • By using variables, Shell programs can provide more flexible functions and stronger adaptability.

2. Shell variable type

Common types of Shell variables include custom variables, environment variables, location variables, and predefined variables

3. User defined variables

3.1 brief introduction

  • Custom variables are variables defined by system users and are only valid in the user's own Shell environment. Therefore, they are also called local variables.

  • When writing Shell scripts, some specific custom variables are usually set to adapt to various changes during program execution and meet different needs.

3.2 definition of variables

  • The variable operation in Bash is relatively simple, not as complex as other high-level programming languages, such as C/C + +, Java, etc

  • When defining a new variable, you generally do not need to declare it in advance, but directly specify the variable name and assign it to the initial value (content)

  • The basic format of defining variables is "variable name = variable value"

  • Variable names start with letters or underscores. The names cannot contain special characters. They are case sensitive. It is recommended to use uppercase for easy identification. There should be no spaces on both sides of the equal sign

3.3 viewing and referencing variable values

  • You can reference the value of a variable by adding the symbol "$" before the variable name

  • You can use the echo command to view variables, and you can view multiple variable values in one echo command at the same time

[root@localhost ~]#echo $name
[root@localhost ~]#name=lisi
[root@localhost ~]#test=22
[root@localhost ~]#echo $name $test 
lisi 22
  • The name of a variable is easily confused with other characters immediately following it. We can use braces "{}" to enclose it. Otherwise, we will not be able to determine the correct variable name. For undefined variables, they will be displayed as null values
[root@localhost ~]#echo $name6.6
[root@localhost ~]#echo ${name}6.6

3.4 special operation of variable assignment

Specifying the variable content directly after the equal sign "=" is the most basic method for assigning values to variables. In addition, some special assignment operations can be used.

  • Double quotation marks: it is allowed to refer to other variable values through the $symbol
[root@localhost ~]#unset py            #Cancel variable name
[root@localhost ~]#echo $py

[root@localhost ~]#py="python $version"
[root@localhost ~]#echo $py
python 2

  • Single quotation mark: it is forbidden to reference other variable values, and $is regarded as an ordinary character
[root@localhost ~]#version=2
[root@localhost ~]#py='python $version'
[root@localhost ~]#echo $py
python $version                            #Cannot call variable
  • Backapostrophe: command replacement, which extracts the output result after the command is executed
[root@localhost ~]#ls -lh which useradd
ls: cannot access which: There is no such file or directory
ls: cannot access useradd: There is no such file or directory
[root@localhost ~]#ls -lh `which useradd`
-rwxr-x---. 1 root root 116K 11 June 2016 /usr/sbin/useradd

#The above operation is equivalent to executing two commands in succession. First, find the location of useradd through the which useradd command, and then list the file attributes according to the search results.
Will be used during execution which useradd The output of the command replaces the entire apostrophe range.
  • Because the apostrophe is difficult to implement the nested command replacement operation in one line of command, you can use "$()" instead of the apostrophe operation to solve the nesting problem.
#Query the location of the configuration file installed by the package that provides the useradd command program

[root@localhost ~]#rpm -qc $(rpm -qf $(which useradd))    
  • read command

The read command is used to prompt the user to enter information, so as to realize a simple interactive process. During execution, a line of content will be read from the standard input device (keyboard), and the read fields will be assigned to the specified variable in turn with a space as a separator (the redundant content will be assigned to the last variable).

If only one variable is specified, the entire line is assigned to this variable.

Case ①
Performing the following operation will wait for the user to input text and assign the input to the variable test

[root@localhost ~]#read test
[root@localhost ~]#echo $test 

In order to make the interactive interface more friendly, the read command can be combined with the "- p" option to set the prompt information to tell the user what to enter.

Case ②

[root@localhost ~]#read -p "please enter your name:" name
 Please enter your name: Ostrovsky
[root@localhost ~]#echo $name
 Nikolai Ostrovsky 
[root@localhost /home]#echo > ip.txt           
[root@localhost /home]#cat ip.txt
[root@localhost /home]#read -p "input your ip:" IP < ip.txt 
[root@localhost /home]#echo $IP

#Read the content from the file and extract it to the variable, eliminating the process of interaction

3.5 setting the action range of variables

  • By default, newly defined variables are only valid in the current Shell environment, so they are called local variables. When entering a subroutine or a new sub Shell environment, local variables can no longer be used.
[root@localhost /home]#name=lisi
[root@localhost /home]#test=20
[root@localhost /home]#bash                #Enter shell environment
[root@localhost /home]#echo $name $test
                                           #Cannot call a variable in the parent shell environment
  • You can export the specified variables as global variables through the internal command export, so that user-defined variables can continue to be used in all sub Shell environments
[root@localhost /home]#exit                #Return to the original shell environment
[root@localhost /home]#export name test    #Set name test as the global variable
[root@localhost /home]#echo $name $test 
lisi 20
[root@localhost /home]#bash                #Enter shell environment
[root@localhost /home]#echo $name $test
lisi 20                                    #Can call

3.6 operation of numerical variables

In Bash Shell environment, only simple integer operation can be performed, and decimal operation is not supported. The operation of integer value is mainly performed through the internal command expr,
There must be at least one space between operator and variable.

The basic format is as follows:

expr Variable 1 operator variable 2 [Operator variable 3]...

Several commonly used operators are shown in the following table

[root@localhost /home]#X=55
[root@localhost /home]#Y=15
[root@localhost /home]#expr $X + $Y
[root@localhost /home]#expr $X - $Y
[root@localhost /home]#expr $X / $Y
[root@localhost /home]#expr $X * $Y                  #Multiplication cannot only use "*", otherwise it will be regarded as a wildcard
expr: syntax error
[root@localhost /home]#expr $X "*" $Y                
[root@localhost /home]#expr $X '*' $Y
[root@localhost /home]#expr $X \* $Y
[root@localhost /home]#expr $X % $Y

Write a simple operation script as follows:

[root@localhost /home]#vim
#1. Define the output number
read -p "Please enter the first number:" num1
read -p "Please enter the second number:" num2
#2. Perform addition operation
sum=`expr $num1 + $num2`

echo "Summation number: $sum"

[root@localhost /home]#chmod +x 
[root@localhost /home]#./ 
Please enter the first number: 22
 Please enter the second number: 88
 Summation number: 110

4. Special variables

4.1 environmental variables

  • Environment variable is a kind of variable created by the system in advance because of operation needs. It is mainly used to set the user's working environment, including user host directory, command search path, user's current directory, login terminal, etc.

  • The value of environment variables is automatically maintained by the Linux system and will change with the change of user status.

  • Configuration files: / etc/profile, ~ /. bash_profile

  • Use the env command to view the environment variables in the current working environment

[root@localhost ~]#env
SSH_CLIENT= 2532 22
......Omitted below
  • The PATH variable is used to set the default executable search PATH. When only the file name is specified to execute the command program, the Linux system will find the corresponding executable file in the directory range specified by the PATH variable. If it cannot be found, it will prompt "command not found"

For example, the XYXY script is located in the home directory. If you want to run the script directly through the file name, you can modify the PATH variable to add the search PATH, or copy the script to a folder of the existing search PATH

[root@localhost /home]#ls -lh ./                         #Confirm the location of the script
-rwxr-xr-x 1 root root 193 9 March 22:29 ./
[root@localhost /home]#echo $PATH                               #View current search path
[root@localhost /home]                                  #Execute directly as file name
bash: Command not found...                                      #Failed, command not found
[root@localhost /home]#PATH="$PATH:/home"                       #Add / home to search path
[root@localhost /home]#echo $PATH                               #Check whether it is added successfully
[root@localhost /home]                                  #Running script with file name succeeded
 Please enter the first number: 1
 Please enter the second number: 2
 Summation number: 3

4.2 read only variables

Used when the variable value cannot be modified

[root@localhost /home]#shuzi=123456
[root@localhost /home]#echo $shuzi 
[root@localhost /home]#shuzi=123
[root@localhost /home]#echo $shuzi 
[root@localhost /home]#readonly $shuzi
bash: readonly: `123': Is not a valid identifier
[root@localhost /home]#readonly shuzi
[root@localhost /home]#echo $shuzi 
[root@localhost /home]#shuzi=123456                #Cannot be modified
bash: shuzi: read-only variable 
[root@localhost /home]#unset shuzi                 #Cannot cancel       
bash: unset: shuzi: Can't unset: read-only variable

4.3 location variables

  • When the command line operation is executed, the first field represents the command name or script program name, and the other string parameters are assigned to the location variable in order from left to right.

  • Location variables, also known as location parameters, are represented by $1, $2, $3... $9

[root@localhost /home]#vim

SUM=`expr $1 + $2`
echo "$1 + $2 = $SUM"

[root@localhost /home]#chmod +x 
[root@localhost /home]#./ 9 9
9 + 9 = 18
[root@localhost /home]#vim                   #Script
[root@localhost /home]#chmod +x              #add permission
useradd $1
echo $2 | passwd --stdin $1
wq Save exit

[root@localhost /home]#./ zhangsan 123456    #Enter user and password
 Change user zhangsan Your password.
passwd: All authentication tokens have been successfully updated.                  #Successfully created zhangsan user

4.4 predefined variables

  • Predefined variables are a kind of special variables pre-defined by Bash program. Users can only use predefined variables, cannot create new predefined variables, and cannot directly assign values to predefined variables

  • The "$" symbol and another symbol are used to represent. Several common predefined variables and their meanings are as follows:

$#Number of location variables in the command line
$*Contents of all location variables
$?The status returned after the execution of the previous command. When the returned status value is 0, it indicates normal execution, and a non-0 value indicates abnormal execution or error
$0Name of the currently executing process / program
$$Indicates that the process number of the current process is returned
$ !Returns the process number of the last background process
$@Indicates that all position parameters are listed, but they are listed in a single form
[root@localhost /home]#ls /tmp/
[root@localhost /home]#echo $?            
0                                            #0 indicates normal execution
[root@localhost /home]#ls /tmp/11
ls: cannot access/tmp/11: There is no such file or directory
[root@localhost /home]#echo $?              
2                                            #2 indicates execution exception


  • Redirection and pipeline operation are very common functions in Shell environment. If you can skillfully master and use them flexibly, it will help to write Shell scripts with concise code but powerful functions.

  • When using expr for calculation, the variable must be an integer, not a string or a decimal, otherwise an error will occur.

Tags: Linux Operation & Maintenance shell

Posted on Fri, 03 Sep 2021 13:49:17 -0400 by prowley