Shell programming beginner bird (V) input and output redirection

Redirect action

A process opened by default standard input, standard output, error output three file descriptors.

Let us redirect the standard output of the program information file to redirect error output, the contents of this file can also be replaced by a standard keyboard as a way to enter.


Redirection symbols

  • Input redirection symbol " <"

  • Output redirection symbol " >", " >>", " 2>", " &>"


Input redirection

01 input redirection symbol " <" role:

Content will file as a parameter input to the process, the following examples:

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

the contents of file.txt file is hello, the above example is the contents of file.txt redirected to athis variable, and the avariable print.


Output redirection

01 output redirection symbol " >" role:

Will file contents emptied , the contents of the output redirected to the specified file, and if the file does not exist is created, the following examples:

[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 " >>" role:

Content output will append to the specified file, the file is not empty, and if the file does not exist it is created, the following examples:

[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 output redirection symbol " 2>" role:

The process is error output content redirected to the specified file, the following examples:

[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

The above results demonstrate that, abc is not a Linux command execution will get an error saying the error message output abd command is not found, then the error message need to use 2>redirection symbol in order to process the contents of error output redirected to the specified file .

04 output redirection symbol " &>" role:

Whether the output process information is correct or incorrect information, it will be redirected to the specified file, the following examples:

[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

Input and output redirection combination redirection

Input and output can also be used in combination, then the composition is mainly used to produce new scene profile which Shell script, the script following examples Shell:

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

The catoutput of the command is redirected to /root/a.sha script file and redirect input EOF is the end of the script. By then execute this script, it will generate a content echo "hello bash"file named a.shscript file.

Results of the:

[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 input and output redirection usage, then we should pay attention to redirect the output includes cover and additional modes, either overwrite or append mode, try not to be used in our system configuration file, before applying it should be noted the system files for backup.

Input and output redirection can also be used in combination, in general, when Shell script to generate among new configuration file, they will use a combination of ways.

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11601021.html