Nine input and output redirection and Shell script file to include

Output redirection : means provided by the system do not use the standard inputs and outputs, but reassigned to other outputs. For example in the string input terminal is supposed to be output to the terminal screen, but the output may be specified in a different file, the input file to the output string, instead of the screen.

Input redirection : is the system does not use standard input ports to provide input, but reassigned to other inputs. For example, could have been inputted through the standard port as an input terminal, the input may be specified as another file, the contents of the file to print output on the terminal screen.

Most Linux system command output and accept input from your terminal to transmit the generated back to your terminal. A command normally read input from a place called standard input by default, which happens to be your terminal. Similarly, a command is usually writes its output to standard output, by default, this is your terminal.


Redirect command list is as follows:

command Explanation
command > file Redirect the output to a file.
command < file Redirect input to file.
command >> file The additional output redirected to the way file.
n > file The file descriptor n file redirected to file.
n >> file The file descriptor n to file additional way redirected to file.
n >& m M and n are the combined output file.
n <& m M and n are the combined input file.
<< tag It will mark the beginning of the content between the tag and the end tag tag as input.

0 Note that the file descriptor is typically standard input (STDIN), 1 is a standard output (STDOUT), 2 is the standard error output (STDERR).


First, output redirection

Redirection is typically done by inserting a specific symbol between commands. In particular, these symbols following syntax:

command1 > file1

The above command will execute command1 and then output the contents into file1, and not output to the terminal screen. Note any file1 already existing content will be replaced with the new content. If you want to add new content at the end of the file, use >> operator.


Examples

Execute the following command who complete it will redirect the output of a command file in the file:

$ who > file

After the execution, the terminal does not output, because the output has been redirected from the default standard output device (terminal) to the specified file.

You can use the cat command to view the contents of the file:

$ cat file
zlkj    console  Oct 31 17:35 
zlkj    ttys000  Dec  1 11:33 

Output redirection overwrites the contents of the file, see the following example:

$ echo "this is test" > file
$ cat users
this is test

If you do not want the contents of the file is overwritten, you can use >> append to a file, for example:

$ echo "this is also test" >> users
$ cat users
this is test
this is also test


Second, input redirection

And as output redirection, Linux commands can also get input from a file, the syntax is:

command1 < file1

In this way, would need to get input from the keyboard command will be transferred to read the contents of the file.

NOTE: output redirection is greater than (>), input redirection is less than (<).


Examples

Then the above example, we need to count the number of rows of file file, execute the following command:

$ wc -l file 
       2 file 

You can also redirect the input file to the file:

$  wc -l < file 
       2 

Note: The above example of two different results: the first example, the file name is output; the second is not, since it only knows the content read from stdin.

command1 < infile > outfile

Alternatively the input and output simultaneously, Command1 execution, reads the content from infile file, and then write the output to the outfile.


三、Here Document

Here Document Shell is in a special manner redirection, to redirect input to an interactive Shell scripts or programs.

Its basic form is as follows:

command << delimiter
    document
delimiter

It is the role of the content between the two delimiter (document) is transmitted as input to the command.

note:

  • ending delimiter must be the top grid write, can not have any characters in front of the back can not have any characters, including spaces and tab indent.
  • Space before and after the start of the delimiter is ignored.


Examples

The number of lines in the command line by calculating Here Document wc -l command:

$ wc -l << EOF
    line1
    line2
    line3
EOF
3          # 输出结果为 3 行


We can also Here Document used in a script, for example:

#!/bin/bash

cat << EOF
    line1
    line2
    line3
EOF

The implementation of the above script, output:

line1
line2
line3


Four, / dev / null file

If you want to execute a command, but do not want to display the output on the screen, you can redirect the output to / dev / null:

$ command > /dev/null

/ dev / null is a special file, its contents will be written to be discarded; if you try to read from the file, then nothing can not read. However, / dev / null file is very useful to redirect the output of a command to it, it will play the effect of "No Output".


Fifth, the file contains

And other languages, Shell may also include an external script. This makes it easy to package some common code as a separate file.

Shell syntax file contains the following:

. filename   # 注意点号(.)和文件名中间有一空格
#或
source filename


Examples

Create two shell script files. test1.sh code is as follows:

#!/bin/bash

url="http://www.cnbolg.com"

test2.sh code is as follows:

#!/bin/bash

#使用 . 号来引用test1.sh 文件
. ./test1.sh

# 或者使用以下包含文件代码
# source ./test1.sh

echo "地址:$url"

Next, we add executable permission to test2.sh and execute:

$ chmod +x test2.sh 
$ ./test2.sh 
地址:http://www.cnbolg.com

Note: The file does not need to be included test1.sh executable permission.


Shell file contains


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11126140.html
Recommended