Objectives
1) Upon completion of this unit, you should be able to:
- Redirect I/O channels to files
- Connect commands using pipes
- Use the for loops iterate over sets of values
Standard Input and Output
1) Linux provides three I/O channels to Programs
- Standard input (STDIN) �C keyboard by default
- Standard output (STDOUT) �C terminal window by default
- Standard error (STDERR) �C terminal window by default
Redirecting Output to a File
1) STDOUT and STDERR can be redirected to files:
- command operator filename
2) Supported operators include:
> Redirect STDOUT to file
2> Redirect STDERR to file
&> Redirect all output to file
3) File contents are overwritten by default.
.>> appends
Redirecting Output to a File Examples
1) This command generates output and errors when run as non-root:
$ find /etc �Cname passwd
2) Operators can be used to store output and errors:
$ find /etc �Cname passwd > find.out
$ find /etc �Cname passwd 2> /dev/null
$ find /etc �Cname passwd > find.out 2> find.err
Redirecting STDOUT to a program (piping)
1) Pipes (the | character) can connect commands:
- command1 | command2
- Sends STDOUT of comand1 to STDIN of command2 instead of the screen.
- STDERR is not forwarded across pipes
2) Used to combine the functionality of multiple tools
- command1 | command2 | command3 …
Redirecting STDOUT to a program Examples:
1) less: view input e page at a time:
$ ls �Cl /etc | less
- Input can be searched with /
2) mail: Send input while email:
3) lpr: Send input to a printer
$ echo “test print” | lpr
$ echo “test print” | lpr �Cp printer_name
Combining Output and Errors
1) Some operators affect both STDOUT and STDERR
&> Redirect all output:
$ find /etc �Cname passwd &> find.all
2>&1: Redirects STDERR to STDOUT
Useful for sending all output through a pipe
$ find /etc �Cname passwd 2>&1 | less
- (): Combines STDOUTs of multiple programs
$ ( cal 2007; cal 2008) | less
Redirecting to Multiple Targets (tee)
1) $ command1 | tee filename | command2
2) Store STDOUT of comand in filename, then pipes to command2
3) Uses:
- Troubleshooting complex pipelines
- Simultaneous viewing and logging of output
Redirecting STDIN from a file
1) Redirect standard input with <
2) Some commands can accept data redirected to STDIN from a file
$ tr ‘A-Z’ ‘a-z’ < .bash_profile
- this command will translate the uppercase characters in .bash_profile to lowercase
3) Equivalent to:
$ cat .bash_profile | tr ‘A-Z’ ‘a-z’
Sending Multiple Lines to STDIN
1) Redirect multiple lines from keyboard to STDIN with <<WORD
- All text until WORD is sent to STDIN
- Sometimes called a heretext
Hi Jane,
Please give me a call when you get in…
Details when you’re site
Thanks,
Jackie
END
Scripting: for loops
1) Performs actions each member of a set of values
2) Example:
for NAME in joe jane julie
do
MESSAGE=’Projects are due today!’
echo $MESSAGE | mail �Cs Reminder $ADDRESS
done
Scripting: for loops continued
1) Can also use command-output and file lists:
- for num in $(seq 1 10)
Assigns 1-10 to $num
seq X Y pirnts the numbers X through Y
- for file in *.txt
Assign names of text files to $file
End of Unit7
1) Questions and Answers
2) Summary
- Standard I/O channels
- File redirection
Standard input (<)
Standard output (>)
Standard Error (2>)
- Pipes redirect standard output to standard input
- for loops can perform commands items from a program’s standard output or an explicit list