< Previous | Contents | Next >
432
5
555
567
5.4. Summary
In this chapter we learned how commands can be linked to each other, and how input from one command can be used as output for another command.
Input/output redirection is a common task on UNIX and Linux machines. This powerful mechanism allows flexible use of the building blocks UNIX is made of.
The most commonly used redirections are > and |. Refer to Appendix C for an overview of redirection commands and other shell constructs.
Table 5-1. New commands in chapter 5: I/O redirection
Command | Meaning |
date | Display time and date information. |
set | Configure shell options. |
sort | Sort lines of text. |
uniq | Remove duplicate lines from a sorted file. |
5.5. Exercises
These exercises give more examples on how to combine commands. The main goal is to try and use the Enter
key as little as possible.
All exercises are done using a normal user ID, so as to generate some errors. While you're at it, don't forget to read those man pages!
• Use the cut command on the output of a long directory listing in order to display only the file permissions. Then pipe this output to sort and uniq to filter out any double lines. Then use the wc to count the different permission types in this directory.
• Put the output of date in a file. Append the output of ls to this file. Send this file to your local mailbox (don't specify anything <@domain>, just the user name will do). When using Bash, you will see a new mail notice upon success.
• List the devices in /dev which are currently used by your UID. Pipe through less to view them properly.
• Issue the following commands as a non-privileged user. Determine standard input, output and error for each command.
♦ cat nonexistentfile
♦ file /sbin/ifconfig
♦ grep root /etc/passwd /etc/nofiles > grepresults
♦ /etc/init.d/sshd start > /var/tmp/output
♦ /etc/init.d/crond start > /var/tmp/output 2>&1
♦ Now check your results by issuing the commands again, now redirecting standardoutput to the file /var/tmp/output and standard error to the file /var/tmp/error.
• How many processes are you currently running?
• How many invisible files are in your home directory?
• Use locate to find documentation about the kernel.
• Find out which file contains the following entry:
root:x:0:0:root:/root:/bin/bash
And this one:
system: root
system: root
• See what happens upon issuing this command:
> time; date >> time; cat < time
• What command would you use to check which script in /etc/init.d starts a given process?