Connecting Filters with Redirection and Piping
Piping the standard output from the first command is sent to the second command as standard input before it goes to the output device % who | more % cat file1 | more == % more file1 % sort file1 | uniq | more % who | grep jp107 | sort | uniq | more ------
Redirection of Standard Input allows you to send the contents of a text file into a utility that expects standard input % mail jplane < file.ltr % mail -s "subject line" jplane < file.ltr ------
Redirection of Standard Output allows you to capture the standard output of a command into a file for use later by creating a new file or replacing the contents of a file that already exists % who > who.list % man vi > man.file ------Redirection of Standard Output to Append allows you to capture the standard output of a command into a file that already exists allows you to append the standard output of a command to the end of a file that already exists or to create the file if it does not % man learn >> man.file
Example with cat % cat > this creates the file getting info from standard input end with ^D % cat this displays the contents to standard output % cat > that creates the file getting info from standard input end with ^D % cat < that displays the contents to standard output (same as % cat that) % cat this that > other creates the file named other with the contents from both this and that % cat other displays the contents to standard output % cat this >> other adds another copy of this to the contents of the file named other % cat other displays the contents to standard output ------tee filter redirection into a file, but a copy also continues down the standard input stream utility producing output------,------> to standard output | | v file Examples % who | tee who.list % who | tee who.1 | more % who | tee who.1 > who.2
Have the same results: % cat f1 | tee f2
% cp f1 f2 % cat f1
%cat f1 > f2 %cat f1
bigger example:
% cat f1 | tee f2 | tee f3 | tee f4 > f5 here document redirection within a script cat << EOF > Working dir $PWD > EOF Working dir /home/user
------noclobber shell variable if it is turned on, it will prevent overwriting a file when you use the redirection of standard output and it will prevent creating a file when you use the appending redirection the ! addition allows you to override the protection when noclobber is turned on
commands: % set noclobber % unset noclobber
Examples: % set noclobber % set noclobber % cat this > that % cat this >> new.file error error % cat this >! that % cat this >>! new.file % unset noclobber % unset noclobber % cat this > that % cat this >> newer.file
Redirection of STDERR
“some command “ &2>1 “some command” &2>/dev/null