shell programming redirection and variables

Posted by brynjar on Fri, 10 Dec 2021 11:57:27 +0100

Due to the particularity of "batch processing" of Shell script, most of its operation processes run silently without user intervention. Therefore, it is very important to learn how to extract and filter execution information. There are two I/O operations in Shell environment: redirection and pipeline.

Linux system uses files to describe various hardware, equipment and other resources, such as hard disk, partition, CD and other equipment files learned before. In the process of processing information through the operating system, users include the following types of interactive device files.
 standard input (STDIN): the default device is the keyboard, the file number is 0, and the command will read the input data required during execution from the standard input file.
 standard output (STDOUT): the default device is the display, the file number is 1, and the command sends the output result after execution to the standard output file.

 standard error (STDERR): the default device is the display, the file number is 2, and the command sends various error messages during execution to the standard error file. Standard input, standard output and standard error use the 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 results to the screen; If there is an error in command execution, the error information will also 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) Redirection output redirection output refers to saving the normal output result of the command to the specified file instead of directly displaying it 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.

//The CPU type information (uname -p) is saved to the kernel Txt file instead of directly displayed on the screen, you can do the following.
`[root@localhost ~]# uname -p > kernel.txt
[root@localhost ~]# cat kernel.txt
x86_64



//Indicates that the specified statistics are appended to the kernel Txt in this file
[root@localhost ~]# uname -r >> kernel.txt
 [root@localhost ~]# cat kernel.txt 
x86_64 3.10.0-514.el7.x86_64 


redirect input

Redirecting input refers to changing the way of receiving input in the command from the default keyboard to the specified file instead of waiting for input from the keyboard. Redirect input usage“<"Operator. 
[root@localhost ~]# vim pass.txt / / add the initial password string "123456"
 123456
  [root@localhost ~]# Passwd -- stdin laoshi < pass. TXT / / get the password 123456 from the pass.txt file and set the password for laoshi
  
  Changing
   password for user jerry. passwd: all authentication tokens updated successfully. 


Error redirection:
After executing some commands, he may report errors and generate some error messages.
So we don't want these error messages to be printed directly on the screen. Therefore, we do an error redirection operation. Store these messages in another 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       

//Save the error information that occurs when you use the tar command for backup to the error.log file.

``Precautions:

When using the "2 >" operator, the contents of the target file will be overwritten as if using the ">" operator. If you want to append contents 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 > Save the two types of output information to different files, or use the "& >" operator to save the two types of output information to the same file. For example, in the automation script for compiling the source package, if you want to ignore the operation process information such as make and make install, you can direct them to the empty file / dev/null and insert the code slice here

[root@localhost ~]# vim httpd_install.sh
 #!/bin/bash # Automatically compile scripts for installing httpd servers 
 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 / / add permission
 
//You can compile and install Apache through shell script
//Note that this http package should be available locally




Pipeline operation pipeline( pipe)Operation provides a mechanism for cooperative work between different commands,
Located in pipe symbol“|"The command output on the left,
As the input (processing object) of the command on the right, multiple pipes can be used in the same command line. 



[root@localhost ~]# grep "/bin/bash$" /etc/passwd / / root:x:0:0:root:/root:/bin/bash bdqn:x:1000:1000:bdqn:/home/bdqn:/bin/bash tsengyia:x:1002:1002::/home/tsengyia:/bin/bash jerry:x:1003:1003::/home/jerry:/bin/bash lisi:x:1004:1004::/home/lisi:/bin/bash 






[root@localhost ~]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}' 
 //After extraction, root /bin/bash bdqn /bin/bash tsengyia /bin/bash jerry /bin/bash lisi /bin/bash 






[root@localhost ~]# df -hT / / before extraction
Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/cl-root xfs 50G 8.0G 43G 16% / devtmpfs devtmpfs 473M 0 473M 0% /dev tmpfs tmpfs 489M 144K 489M 1% /dev/shm tmpfs tmpfs 489M 14M 476M 3% /run tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 173M 842M 18% /boot /dev/mapper/cl-home xfs 42G 33M 42G 1% /home tmpfs tmpfs 98M 24K 98M 1% /run/user/0 /dev/sr0 iso9660 4.1G 4.1G 0 100% /media/cdrom [root@localhost ~]# df -hT | grep "/$" | awk '{print }' / / after extraction, where grep "/ $" means to extract the line ending with "/"
 16% 
 

Topics: Linux bash server