Bird Initial Shell Programming Input-Output Redirection

Keywords: Linux shell

Redirection role

A process opens three file descriptors, Standard Input, Standard Output, and Error Output, by default.

Redirection can redirect information from our program's standard output and error output to the file, so here you can also use the contents of the file instead of the keyboard as a standard input.

Redirection Symbol

  • Enter redirection symbol'<'
  • Output redirection symbols'>', >', >', >'.

Input redirection function

01 Enter the role of the redirection symbol'<':

The contents of the file are entered into the process as parameters, for example:

[root@omp120 home]# cat file.txt 
hello
[root@omp120 home]# read a < file.txt 
[root@omp120 home]# echo $a
hello

The contents of the file.txt file are hello, and the example above is to redirect the contents of the file.txt to the variable a and print out the variable a.

Output redirection function

01 Role of output redirection symbol'>':

The contents of the file are emptied, the output is redirected to the specified file, and the file is created if it does not exist, as in the following example:

[root@lincoding tmp]# echo 123 > /tmp/test
[root@lincoding tmp]# cat /tmp/test 
123
[root@lincoding tmp]# echo abc > /tmp/test
[root@lincoding tmp]# cat /tmp/test 
abc
02 Output redirection symbol ">":

The output will be appended to the specified file, which will not be emptied and created if it does not exist, as in the following example:

[root@lincoding tmp]# echo 123 >> /tmp/test
[root@lincoding tmp]# cat /tmp/test
123
[root@lincoding tmp]# echo abc >> /tmp/test
[root@lincoding tmp]# cat /tmp/test 
123
abc
03 Role of output redirection symbol "2>":

Redirects the output of a process error to a specified file, as shown in the following example:

[root@lincoding home]# abc 
-bash: abc: command not found
[root@lincoding home]# abc > error.txt
-bash: abc: command not found
[root@lincoding home]# cat error.txt 
[root@lincoding home]# 
[root@lincoding home]# abc 2> error.txt
[root@lincoding home]# cat error.txt 
-bash: abc: command not found

As you can see from the above demonstration, abc is not a Linux command, and it executes an error message output that will error that the abd command cannot find. This error message requires a 2>redirector to redirect the contents of the process error output to the specified file.

04 Output redirection symbol'&>'role:

Whether the information output by the process is correct or incorrect, it is redirected to the specified file, as in the following example:

[root@lincoding home]# abc &> file.txt
[root@lincoding home]# cat file.txt 
-bash: abc: command not found
[root@lincoding home]# free -m &> file.txt
[root@lincoding home]# cat file.txt 
             total       used       free     shared    buffers     cached
Mem:           980        918         62          0         71        547
-/+ buffers/cache:        299        681
Swap:         1983          0       1983

Combining input redirection with output redirection

Input and output can also be combined, so this combination is mainly used in scenarios where a new configuration file is generated in a shell script, such as the Shell script example:

#!/bin/bash
cat > /home/a.sh << EOF
echo "hello bash"
EOF

Redirect the output of the cat command to the / root/a.sh script file, and end the script with input redirection.By executing this script, a script file named a.sh with echo "hello bash" is generated.

Execution results:

[root@lincoding home]# ./test.sh 
[root@lincoding home]# ls -l a.sh 
-rw-r--r--. 1 root root 18 Sep 27 16:41 a.sh
[root@lincoding home]# chmod u+x a.sh 
[root@lincoding home]# cat a.sh 
echo "hello bash"
[root@lincoding home]# ./a.sh 
hello bash

Summary

The above content is about the use of input and output redirection, so you should pay attention to the output redirection including overwrite and append mode, whether overwrite or append mode, try not to use in our system configuration files, then we should pay attention to backing up the system files before application.

Input and output redirection can also be used in combination, typically when generating new configuration files in Shell scripts.

Posted by hamzatki on Fri, 27 Sep 2019 09:08:32 -0700