Shell programming specification and the pipe (|) and redirection (< ,>,>>) in the shell

Application scenarios of Shell script

Shell script (Shell Script) is a program file that commands to be executed are saved to a text file in order, and executable permissions are given to the file to facilitate one-time execution. Mainly to facilitate the administrator to set up or manage, can combine various Shell control statements to complete more complex operations. Commonly used in repetitive operations, batch transaction processing, automated operation and maintenance, service operation status monitoring, timing task execution, etc.
In some complex Linux maintenance tasks, a large number of repetitive input and interactive operations are not only time-consuming and laborious, but also error-prone. Writing an appropriate Shell script program can batch process and automatically complete a series of maintenance tasks, which greatly reduces the administrator Burden.
Like the website publishing script, we log on to the website every day, and we will find that the content of each page is not static. Under normal circumstances, the website will regularly update the content of the website according to the code developed by the developer. This is called the website regularly releasing new versions. However, for some websites with a relatively short update interval, manually executing the command release is a repetitive operation, which is time-consuming and troublesome. In order to solve this problem, an automatic release version can be developed, and the script can be released efficiently, accurately and easily with one click.

Shell programming specification

The Shell script in the Linux system is a special application program, which is between the operating system kernel and the user. It acts as a translator. It is responsible for receiving and interpreting the operation commands entered by the user, and passing the operations that need to be performed to the kernel Execute and output the execution result

1. Types of Shell interpreter programs

There are many types of common shell interpreter programs. When using different shell scripts, there will be some differences in their internal instructions, command line prompts, etc. Through the /etc/shells file, you can understand the types of Shell scripts supported by the current system.
Among them, /bin/bash is the default Shell script used by most Linux versions. The full name of Bash is Bourne Again Shell, which is currently one of the most popular open source software projects.

[root@localhost ~]# cat /etc/shells    //当前系统所支持的Shell脚本种类
/bin/sh
/bin/bash         //最受欢迎的开源软件项目之一
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
2. Write a simple Shell script
[root@localhost ~]# vi ceshi.sh   //创建编辑一个以.sh结尾的文件
#!/bin/bash
#一个简单的Shell脚本        //“#”用于注释的内容
cd/opt/                                    //切换到opt目录
echo "当前位于哪个目录:"    //在屏幕显示“ ....... ”
pwd                                          //查看当前在那个目录中
echo "查看当前目录:"      
ls -lh                                         //查看当前目录内容 
3. The way to execute the script file

First, add execution permissions to the script (important):

[root@localhost ~]# chmod +x ceshi.sh    //给文件添加执行的权限

3.1. According to the path of the script file (relative path and absolute path)

[root@localhost ~]# ./ceshi.sh    //或  .ceshi.sh 前提是在执行文件的目录下
当前位于哪个目录:
/opt
查看当前目录:
总用量 0
drwxr-xr-x. 2 root root 6 3月  26 2015 rh

3.2, sh script file path

[root@localhost /]# sh /root/ceshi.sh  //执行脚本
当前位于哪个目录:
/opt
查看当前目录:
总用量 0
drwxr-xr-x. 2 root root 6 3月  26 2015 rh

3.3, Source script file path

[root@localhost /]# source /root/ceshi.sh   //刷新执行脚本
当前位于哪个目录:
/opt
查看当前目录:
总用量 0
drwxr-xr-x. 2 root root 6 3月  26 2015 rh
[root@localhost opt]# 

4. In the Shell environment: pipeline (|) and redirection (<, >, >>)

Due to the particularity of shell script batch processing, most of its operations are in the background and do not require user intervention. Therefore, learning to extract and filter execution information becomes very important.

4.1. Application of pipe symbol (|)

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

cmd1  命令1 |  cmd2  命令2  [... | cmdn 命令 n]   //管道符号的使用格式。22

In shell script applications, pipeline operations are usually used to filter the key information needed. For example, when you use the grep command to check /bin/bash as the system user name of the Shell, the entire line that meets the conditions will be output. On this basis, you can combine the pipeline operation with the awk command for further filtering, and only output the user name and login Shell.

[root@localhost opt]# grep "/bin/bash$" /etc/passwd    //过滤文件中以/bin/bash结尾的行
root:x:0:0:root:/root:/bin/bash
[root@localhost opt]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}'      
        //过滤该行以冒号分割的第一位与第七位
root /bin/bash
[root@localhost opt]# free -m    //查看内存使用使用情况
total 内存总数:7806M
used 已经使用的内存数: 463M
free 空闲的内存数: 6760M
shared 当前已经废弃不用,总是10M
buffers Buffer 缓存内存数: 582M
cached Page 缓存内存数:582M
available     实际可用的内存数:7055M
              total        used        free      shared  buff/cache   available
Mem:           7806         463        6760          10         582        7055
Swap:          8063           0        8063
[root@localhost opt]# free -m |grep "Mem:" |awk '{print $4}'    //过滤内存空闲的内存数
6760
4.2. Application of redirection symbols (<, >, >>)

The Linux system uses files to describe various hardware, equipment and other resources, such as the hard disk and partition, CD-ROM and other device files learned before. The process of user processing information through the operating system includes the following types of interactive device files.

  • Standard input (STDIN): The default device is the keyboard, and the file number is 0. The command will read the input data needed during execution from the standard input file.

  • Standard output (STDOUT): The default device is a display, the file number is 1, the command will send the output result after execution to the standard output file.

  • Standard Error (STDERR): The default device is the display, and the file number is 2. The command sends various error messages during execution to the standard error file.

    Standard input, standard output and standard error use keyboard and display as associated devices by default to interact with the operating system to complete the most basic input and output operations, that is, receive various command strings and auxiliary control information input by the user from the keyboard. And output the command result to the screen; if the command execution makes an error, the error message will be fed back to the screen.

    In actual Linux system maintenance, you can change the direction of input and output content without using the default standard input and output devices (keyboard and display). This operation is called redirection.

(1), redirect output

Redirecting output refers to saving the normal output result of the command to a specified file instead of directly displaying it on the monitor screen. The redirected output uses the ">" or ">>" operation symbols to overwrite or append files, respectively.

If the target file for redirected output does not exist, the file will be created and the output result of the previous command will be saved to the file; if the target file already exists, the output result will be overwritten or appended to the file. For example, if you want to save the CPU type information (uname -p) of the current host to the kernel.txt file instead of directly displaying it on the screen, you can perform the following operations.

[root@localhost ~]# uname -p > kernel.txt
[root@localhost ~]# cat kernel.txt
x86_64

When you need to keep the original content of the target file, you should use the ">" operation symbol instead to append the content instead of overwriting it. For example, perform the following operations to append the kernel version information to the kernel.txt file.

[root@localhost ~]# uname -r >> kernel.txt    
[root@localhost ~]# cat kernel.txt
x86_64
3.10.0-514.el7.x86_64
(2) Redirect input

Redirecting input refers to changing the way of receiving input in a command from the default keyboard to a specified file instead of waiting for input from the keyboard. Redirect input uses 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, you must enter the password string twice as prompted each time, which is very cumbersome. If you switch to redirect input, you can omit the interactive process and automatically complete the password setting (combined with the passwd command "–Stdin" option to identify standard input).

[root@localhost ~]# vim pass.txt	//添加初始密码串内容"123456" 123456
[root@localhost ~]# passwd --stdin jerry < pass.txt
//从pass.txt 文件中取密码,需要注意 SELinux 会影响此命令执行,若执行失败可尝试关闭 SELinux 
Changing password for user jerry.
passwd: all authentication tokens updated successfully.

Non-interactive command statements can be used more conveniently in Shell scripts, which greatly reduces the interruption of the program and improves the efficiency of script execution.

(3), wrong redirection

Error redirection refers to saving the error information (such as options or parameter errors, etc.) that occur 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 number of the error file (when using standard output and standard input redirection, the number 1, 0 is actually omitted).

In actual applications, error redirection can be used to collect error information about program execution and provide a basis for troubleshooting; for shell scripts, you can also redirect insignificant error information to the empty file /dev/null to maintain script output Succinct. For example, you can perform the following operations to save the error information that occurs during backup using the tar command to the error.log file.

[root@localhost ~]# tar jcf /nonedir/etc.tgz /etc/ 2> error.log
[root@localhost ~]# cat error.log
tar: Removing leading `/' from member namestar (child): /nonedir/etc.tgz: Cannot open
: No such file or directory
tar (child): Error is not recoverable: exiting now

When using the "2>" operator, the content of the target file will be overwritten like the ">" operator. If you want to append content instead of overwriting the file, you should use the "2>>" operator instead.

When the result of the command output may include both standard output (normal execution) information and error output information, you can use the operator ">" "2>" to save the two types of output information to different files, or use " The &>" operator saves the two types of output information to the same file. For example, in an automated script that compiles the source code package, if you want to ignore the operation process information such as make and make install, you can direct it to the empty file /dev/null.

[root@localhost ~]# vim httpd_install.sh
#!/bin/bash
# 自动编译安装httpd 服务器的脚本
cd /usr/src/httpd-2.4.25/
./configure --prefix=/usr/local/httpd --enable-so &> /dev/null make &> /dev/null
make install &> /dev/null
…… //省略部分内容
[root@localhost ~]# chmod +x httpd_install.sh

Guess you like

Origin blog.csdn.net/wulimingde/article/details/107919456