Input-Output Redirection Flashcards
Redirect standard output to a file
echo “test” > file.txt
Redirect and append standard output to a file
echo “test”»_space; file.txt
Chain scripts, files, and commands together by STDOUT as STDIN for the next command
|
cat /etc/passwd | grep root
Redirect standard error
2>
Redirect STDERR to STDOUT
2>&1
Accept input from file
<
grep -i “ERROR” < /var/log/messages
What is a “here document”
A way to run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script
How do you use a here document
The general form for a here document is − command << delimiter document delimiter The following script runs a session with the vi text editor and saves the input in the file test.txt. #!/bin/sh
filename=test.txt
vi $filename <
Describe each file descriptor
&0 is STDIN
&1 is STDOUT
&2 is STDERR
Redirect STDOUT to STDERR
echo “test” >&2