80 likes | 216 Views
CSCI 330 The UNIX System. Shell Data Handling: Redirection and Piping. Output. The output statement of the shell is the “echo” command Syntax: echo [option] arg1 arg2 …argN its arguments can be strings or variables option “-n” will suppress trailing newline. Output with echo. Examples:
E N D
CSCI 330The UNIX System Shell Data Handling: Redirection and Piping
CSCI 330 - The UNIX System Output • The output statement of the shell is the “echo” command Syntax: echo [option] arg1 arg2 …argN • its arguments can be strings or variables • option “-n” will suppress trailing newline
CSCI 330 - The UNIX System Output with echo Examples: % echo “Hello Ray" Hello Ray % echo “Hello $USER" Hello a132436 % echo “It is now `date`” It is now Mon Feb 25 10:24:08 CST 2008
CSCI 330 - The UNIX System Output Redirection (>) • Syntax: command > file Sends output of command to file, instead of to terminal • Examples: % du > status % (date; du) > status Calls the disk usage command for the current directory and redirects the output to a file called ‘status’ ( ) indicates command groups. Use it to combine the output of multiple commands. In this example, we place time and date in front of the disk usage
CSCI 330 - The UNIX System Input Redirection (<) • Syntax: Command < file Command will read (take input) from file, instead of from terminal • Example: % tr “[A-Z]” “[a-z]” < report.input
CSCI 330 - The UNIX System Examples: Output / Input • Redirecting input and output: % tr ‘[A-Z]’ ‘[a-z]’ < r.in > r.out • Output of command becomes input to next: % ls > temp.txt; wc < temp.txt • Eliminate the middleman: pipe % ls | wc
CSCI 330 - The UNIX System Build the file ‘usage-status’ from the output of the ‘date’, ‘ls’, and ‘du’ commands Appending Output • Syntax: command >> file adds output of command at the end of file • If file does not exist, shell creates it • Examples: % date > usage-status % ls -l >> usage-status % du -s >> usage-status
CSCI 330 - The UNIX System Summary: Redirections and Pipe