惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - tony

The fastest way going abroad... Love is meeting the requirments, too... Fight back and hold back... Section 14: The UNIX File System Section 13: Interactive Use of the Shell Section 12: Customizing the UNIX Shell Section 11: The Execution Environment Section 10: Editing Text with EMACS Section 9: Interaction and Job Control Section 8: Process Control and Multitasking Section 7: Pipelines and Filters Section 5: Working with Files and Directories Section 4: The UNIX Shell Section 3: Logging In and Logging Out Section 2: Accessing a UNIX System Section 1: What Is UNIX? An overview about our Martch work... Obesession... Oh,,,CHANEL...
Section 6: Redirecting Input and Output
tony · 2005-03-31 · via 博客园 - tony

CONCEPT: Every program you run from the shell opens three files: Standard input, standard output, and standard error. The files provide the primary means of communications between the programs, and exist for as long as the process runs.

The standard input file provides a way to send data to a process. As a default, the standard input is read from the terminal keyboard.

The standard output provides a means for the program to output data. As a default, the standard output goes to the terminal display screen.

The standard error is where the program reports any errors encountered during execution. By default, the standard error goes to the terminal display.

CONCEPT: A program can be told where to look for input and where to send output, using input/output redirection. UNIX uses the "less than" and "greater than" special characters (< and >) to signify input and output redirection, respectively.

Redirecting input

Using the "less-than" sign with a file name like this:

< file1

in a shell command instructs the shell to read input from a file called "file1" instead of from the keyboard.

EXAMPLE:Use standard input redirection to send the contents of the file /etc/passwd to the more command:

more < /etc/passwd

Many UNIX commands that will accept a file name as a command line argument, will also accept input from standard input if no file is given on the command line.

EXAMPLE: To see the first ten lines of the /etc/passwd file, the command:

head /etc/passwd

will work just the same as the command:

head < /etc/passwd

Redirecting output

Using the "greater-than" sign with a file name like this:

> file2

causes the shell to place the output from the command in a file called "file2" instead of on the screen. If the file "file2" already exists, the old version will be overwritten.

EXAMPLE: Type the command

ls /tmp > ~/ls.out

to redirect the output of the ls command into a file called "ls.out" in your home directory. Remember that the tilde (~) is UNIX shorthand for your home directory. In this command, the ls command will list the contents of the /tmp directory.

Use two "greater-than" signs to append to an existing file. For example:

>> file2

causes the shell to append the output from a command to the end of a file called "file2". If the file "file2" does not already exist, it will be created.

EXAMPLE: In this example, I list the contents of the /tmp directory, and put it in a file called myls. Then, I list the contents of the /etc directory, and append it to the file myls:

ls /tmp > myls
ls /etc >> myls

Redirecting error

Redirecting standard error is a bit trickier, depending on the kind of shell you're using (there's more than one flavor of shell program!). In the POSIX shell and ksh, redirect the standard error with the symbol "2>".

EXAMPLE: Sort the /etc/passwd file, place the results in a file called foo, and trap any errors in a file called err with the command:

sort < /etc/passwd > foo 2> err