$ grep "hello" >junk.txt Now is the time for every good person to say hello. (type control-D here) $ cat junk.txt say hello. $ Input and output redirection, and the use of input files on the command line instead of redirected input:
$ grep "Now" <hello.txt >junk.txt $ grep "Now" hello.txt >junk.txt Appending additional data to a file using an output redirection:
$ echo "Now is the time" >hello.txt $ echo "for every good person to" >>hello.txt $ echo "say hello." >>hello.txt $ cat hello.txt Now is the time for every good person to say hello. $
Redirecting standard output and standard error, and redirecting standard error to the
$ find / -name *.txt -exec ls -l {} \; 2>/dev/null >textfiles
$
Basic pipes:
$ grep "hello" < hello.txt | sed -e "s/hello/bye/" > result.txt $( grep "hello" | sed -e "s/hello/bye/" ) < hello.txt > result.txt $
I also stated that redirecting output to an existing file would delete the file and create a new version of it. In the following example, the fourth line causes
$ echo "hello" >hello.txt $ cat hello.txt hello $ echo "bye" >hello.txt $ cat hello.txt bye
You can set the
$ set noclobber $ echo "hello" >hello.txt $ cat hello.txt hello $ echo "bye" >hello.txt File "hello.txt" already exists $ cat hello.txt hello unset noclobber
If
$ set noclobber $ echo "hello" >|hello.txt $ cat hello.txt hello $ echo "bye" >|hello.txt $ cat hello.txt bye unset noclobber
Combining standard output and standard error
$ find / -name *.txt -exec ls -l {} \; >found.txt
find: /some/directory: Permission denied
find: /another/one: Permission denied
$
The redirection operator is actually a number followed by the redirection symbol, as in the following example. If number is omitted, 1 is the default.
$ find / -name *.txt -exec ls -l {} \; 1>found.txt
$
The following commands are equivalent:
$ find / -name *.txt -exec ls -l {} \; 1>found.txt
$ find / -name *.txt -exec ls -l {} \; >found.txt
$
Unix utilities open three files automatically when a program starts up. These files are given file descriptor numbers inside the program -- 0, 1, and 2 -- but they're more commonly known as stdin (standard input -- file descriptor 0), stdout (standard output -- file descriptor 1), and stderr (standard error -- file descriptor 2). When the program starts, default assignments for these files are made to
The following examples use One method of handling the logging problem would be to create separate logs for each of the outputs, as in the following example.
$ find / -name *.txt -exec ls -l {} \; 1>found.txt 2>errors.txt
$
It is also possible to redirect an output by attaching it to an already open redirection using the
$ find / -name *.txt -exec ls -l {} \; 1>result.txt 2>&1
$
The order of redirection is important. In the following example, the output of file descriptor 2 (standard error) is attached to file descriptor 1. At this point, standard output is still attached to the terminal, so standard error is sent to the terminal. The next redirection sends standard output to
$ find / -name *.txt -exec ls -l {} \; 2>&1 1>result.txt
find: /some/directory: Permission denied
find: /another/one: Permission denied
$
Input redirection from here documents
First, create a text file with several
sample hello.txt hello world hello broadway hello dolly
Create a file named
# here.sh - sample here document vi hello.txt <<END-OF-INPUT :g/hello/s//bye/g :w :q! END-OF-INPUT echo "Editing complete" Change the mode on the file to make it executable:
$ chmod a+x here.sh
When you execute the
$ ./here.sh Vim: Warning: Input is not from a terminal Editing complete $ cat hello.txt sample bye.txt bye world bye broadway bye dolly
If you really want to suppress the vi warning, redirect the error to the
# here.sh - sample here document vi hello.txt 2>/dev/null <<END-OF-INPUT :g/hello/s//bye/g :w :q! END-OF-INPUT echo "Editing complete"
# here.sh - sample here document vi hello.txt 2>/dev/null <<-STOP-HERE :g/hello/s//bye/g :w :q! STOP-HERE echo "Editing complete"
Because it's an interactive program, the
# xfr.sh - Transfers to a remote system
district=nj
ftplog=xfr.log
insbase=/usr/installations
insdir=$insbase/new
inskit=newstuff.a
echo "Transferring to" $district
ftp 1>>$ftplog 2>&1 $district"_system" <<-ALL-DONE
user mo ddd789
binary
mkdir $insbase
chmod 777 $insbase
mkdir "$insdir"
chmod 777 $insdir
put $inskit $insdir/$inskit
chmod 777 $insdir/$inskit
bye
ALL-DONE
echo "Transfer to" $district "complete."
The first file would have to contain nothing but the commands for
user mo ddd789 binary mkdir /usr/installations chmod 777 /usr/installations mkdir /usr/installations/new chmod 777 /usr/installations/new put newstuff.a /usr/installations/new/newstuff.a chmod 777 /usr/installations/new /newstuff.a bye # xfr.sh - Transfers to a remote system district=nj ftplog=xfr.log echo "Transferring to" $district ftp 1>>$ftplog 2>&1 $district"_system" <ftp_commands echo "Transfer to" $district "complete." In our next installment, I'll cover Unix system and global variables. What are they and how do you use them? I have been meaning to do this one for a while, and now seems like a good time. |
|
||||||||||||||||||||||||||||||||||||||||||||
|
Copyright©2001 King Computer Services Inc. All rights reserved. |
|||||||||||||||||||||||||||||||||||||||||||||