1. PLANTEAMIENTO DEL PROBLEMA
6.2 MARCO TEORICO
6.2.5 Pensamiento científico
You can now wander about the file system, find files that might be of interest, check their types with file, and even view them with the cat command, but what if they’re longer than the size of your screen? That’s the job of the more program, a program that knows how big your screen is and displays the information page by page.
There are three primary flags in more:
-s Suppresses multiple blank lines, just like the -s flag to cat
-d Forces more to display friendlier prompts at the bottom of each page -c Causes the program to clear the screen before displaying each screen full
of text
The program also allows you to start at a specific line in the file by using the curious +n
notation, where n is a specific number. Finally, you can start also at the first occurrence of
a specific pattern by specifying that pattern to the program in a format similar to +/pattern
(patterns are defined in Hour 9).
1. View the .cshrc file using more: % more ~/.cshrc
#
# Default user .cshrc file (/bin/csh initialization). set host=limbo
set path=(. ~/bin /bin /usr/bin /usr/ucb /usr/local /etc /usr/etc /usr/local/bin /usr/unsup/bin)
# Set up C shell environment:
alias diff ‘/usr/bin/diff -c -w’ alias from ‘frm -n’
alias ll ‘ls -l’ alias ls ‘/bin/ls -F’ alias mail Mail
alias mailq ‘/usr/lib/sendmail -bp’ alias newaliases ‘echo you mean newalias...’
7
Unlike previous examples where the program runs until completed, leaving you back on the command line, more is the first interactive program you’ve encountered. When you see the --More--(51%) prompt, the cursor sits at the end of that line, waiting for you to tell it what to do. The more program lets you know how far into the file you’ve viewed, too; in the example, you’ve seen about half of the file (51 percent).
At this point, there is quite a variety of commands available. Press the spacebar to see the next screen of information, until you have seen the entire file.
2. Try starting up the program with the twelfth line of the file: % more +12 ~/.cshrc
alias mailq ‘/usr/lib/sendmail -bp’ alias newaliases ‘echo you mean newalias...’ alias rd ‘readmsg $ | page’
alias rn ‘/usr/local/bin/rn -d$HOME -L -M -m -e -S -/’ # and some special stuff if we’re in an interactive shell if ( $?prompt ) then # shell is interactive. alias cd ‘chdir \!* ; setprompt’
alias env ‘printenv’
alias setprompt ‘set prompt=”$system ($cwd:t) \! : “‘ set noclobber history=100 filec
umask 007 setprompt endif
--More--(82%)
3. You can see that about halfway through the .cshrc file there is a line that contains the word newaliases. I can start up more so that the line with this pattern is displayed on the top of the first screenful.
% more +/newaliases ~/.cshrc
...skipping
alias mailq ‘/usr/lib/sendmail -bp’ alias newaliases ‘echo you mean newalias...’ alias rd ‘readmsg $ | page’
alias rn ‘/usr/local/bin/rn -d$HOME -L -M -m -e -S -/’ # and some special stuff if we’re in an interactive shell if ( $?prompt ) then # shell is interactive.
7
alias cd ‘chdir \!* ; setprompt’ alias env ‘printenv’
alias setprompt ‘set prompt=”$system ($cwd:t) \! : “‘ set noclobber history=100 filec
umask 007 setprompt endif
# special aliases: alias info ssinfo --More--(86%)
Actually, notice that the line containing the pattern newaliases shows up as the third line of the first screen, not the first line. That’s so you have a bit of context to the matched line, but it can take some getting used to. Also note that more tells us—with the message ...skipping as the very first line—that it’s skipping some lines to find the pattern.
4. The range of commands available at the --More-- prompt is quite extensive, as listed in Table 7.1. The sidebar following the table explains what the conventions used in the table mean and how to enter the following commands.
Table 7.1. Commands available within the more program.
Command Function
[Space] Press the spacebar to display the next screenful of text.
n[Return] Display the next n lines (the default is the next line only of text).
h Display a list of commands available in the more program. d Scroll down half a page.
q Quit the more program.
ns Skip forward n lines (default is 1). nf Skip forward n screenfuls (default is 1).
b or Control-b Skip backward a screenful of text. = Display the current line number. /pattern Search for an occurrence of a pattern.
n Search for the next occurrence of the current pattern. v Start the vi editor at the current line.
7
Entering Commands in the more Program
In this table and in the following text, the word space enclosed in brackets [Space] refers to pressing the spacebar as a command. Likewise, [Return] means you should press the Return key as part of the command.
A hyphen in a command—for example Ctrl-b—means that you should hold down the first indicated key while you press the second key. The lowercase-letter com- mands in the table indicate that you should press the corresponding key, the A key for the a command, for example.
Two characters together, but without a hyphen (:f), mean that you should press the appropriate keys in sequence as you would when typing text.
Finally, entries that have an n before the command mean that you can prefix the
command with a number, which will let it use that value to modify its action. For example, 3[Return] displays the next three lines of the file and 250s skips the next 250 lines. Typically, pressing Return after typing a command within more is not necessary.
Try some commands on a file of your own. A good file that will have enough lines to make this interesting is /etc/passwd:
% more /etc/passwd
root:?:0:0: root:/:/bin/csh
news:?:6:11:USENET News:/usr/spool/news:/bin/ksh ingres:*?:7:519:INGRES Manager:/usr/ingres:/bin/csh usrlimit:?:8:800:(1000 user system):/mnt:/bin/false vanilla:*?:20:805:Vanilla Account:/mnt:/bin/sh charon:*?:21:807:The Ferryman:/users/tomb:
actmaint:?:23:809: Maintenance:/usr/adm/actmaint:/bin/ksh pop:*?:26:819:,,,,:/usr/spool/pop:/bin/csh
lp:*?:70:10:System V Lp Admin:/usr/spool/lp:
trouble:*?:97:501:Trouble Report Facility:/usr/trouble:/usr/msh postmaster:?:98:504:Mail:/usr/local/adm:/bin/csh aab:?:513:1233:Robert Townsend:/users/aab:/bin/ksh billing:?:516:1233:Accounting:/users/billing:/bin/csh aai:?:520:1233:Pete Cheeseman:/users/aai:/bin/csh --More--(1%) 60s ...skipping 60 lines cq:?:843:1233:Rob Tillot:/users/cq:/usr/local/bin/tcsh robb:?:969:1233:Robb:/users/robb:/usr/local/lib/msh aok:?:970:1233:B Jacobs:/users/aok:/usr/local/lib/msh went:?:1040:1233:David Math:/users/went:/bin/csh aru:?:1076:1233:Raffie:/users/aru:/bin/ksh varney:?:1094:1233:/users/varney:/bin/csh brandt:?:1096:1233:Eric Brand:/users/brand:/usr/local/bin/tcsh ask:?:1098:1233:/users/ask:/bin/csh asn:?:1101:1233:Ketter Wesley:/users/asn:/usr/local/lib/msh --More--(2%)
7
This example isn’t exactly what you’ll see on your screen because each time you type a command to more, it erases its own prompt and replaces the prompt with the appropriate line of the file. Try pressing [Return] to move down one line, and you’ll see what I mean.
Quit more in the middle of viewing this file by typing q.
The more program is one of the best general-purpose programs in UNIX, offering an easy and powerful tool for perusing files. The biggest limitation, however, is that you can’t go backward in the file: If you realize that what you are looking for is on the previous page, you have to quit and start the program again.
Summary
Now that you can add this set of commands to your retinue of UNIX expertise, you are most certainly ready to wander about your own computer system, understanding what files are what, where they are, and how to peer inside. You learned about file to ascertain type, head and tail for seeing snippets of files, and cat and more to help easily view files of any size on your screen.
Workshop
The Workshop summarizes the key terms you learned and poses some questions about the topics presented in this chapter. It also provides you with a preview of what you will learn in the next hour.
Key Terms
block special device A device driver that controls block-oriented peripherals. A hard disk,
for example, is a peripheral that works by reading and writing blocks of information (as distinguished from a character special device). See also character special device.
character special device A device driver that controls a character-oriented peripheral. Your keyboard and display are both character-oriented devices, sending and displaying information on a character-by-character basis. See also block special device.
control-key notation A notational convention in UNIX that denotes the use of a control key. There are three common conventions: Ctrl-C, ^c and C-C all denote the Control-c character, produced by pressing the Control key (labeled Control or Ctrl on your keyboard) and, while holding it down, pressing the c key.
core dump The image of a command when it executed improperly.
7
major number For device drivers, the major number identifies the specific type of device
in use to the operating system. This is more easily remembered as the device ID number.
minor number Once the device driver is identified to the operating system by its major
number, the address of the device in the computer itself (that is, which card slot a peripheral card is plugged into) is indicated by its minor number.
null character Each character in UNIX has a specific value, and any character with a
numeric value of zero is known as a null or null character.
pipeline A series of UNIX commands chained by |, the pipe character.
Questions
1. Many people who use UNIX systems tend to stick with file-naming conventions. Indeed, UNIX has many of its own, including .c for C source files, .Z for com- pressed files, and a single dot prefix for dot files. Yet file ignores filenames (test it yourself). Why?
2. Use more to check some of the possible file types that can be recognized with the file command by peeking in the configuration file /etc/magic.
3. Do you remember the television game show “Name that Tune?” If so, you’ll recall how contestants had to identify a popular song by hearing just the first few notes. The file command is similar; the program must guess at the type of the file by checking only the first few characters. Do you think it would be more accurate by checking more of the file, or less accurate? (Think about this one.)
4. How did the cat command get its name? Do you find that a helpful mnemonic? 5. Here’s an oddity: What will this command do?
cat LISTS | more
6. If you were looking at an absolutely huge file and you were pretty sure that what you wanted was near the bottom, what command would you use, and why? 7. What if the information is near the top?
Preview of the Next Hour
There are lots of special characters in UNIX, as you have doubtless learned by accidentally typing a slash, asterisk, question mark, quote, or just about any other punctuation character. What may surprise you is that they all have different, specific meanings. The next hour explains considerably more about how pipelines work and how programs are used as filters. Among the new commands you will learn are sort, wc, nl, uniq, and spell. You also will learn a new, immensely helpful flag to cat that makes cat produce line numbers.
5
8
Hour
8
Filters and Piping
If you’ve ever learned a foreign language, you know that the most common approach is to start by building your vocabulary (almost always including the names of the months, for some reason), and then you learn about sentence construction rules. The UNIX command line is a lot like a language. Now you’ve learned a lot of UNIX words, so it’s time to learn how to put them together as sentences using file redirection, filters, and pipes.
Commands to be added to your vocabulary this hour include wc, sort, nl, and uniq. You also learn about the -n flag to the cat command, which forces cat to add line numbers, and how you can use that to help find information within files.
Goals for This Hour
In this hour, you learn
■ The secrets of file redirection
■ How to count words and lines using wc
■ How to remove extraneous lines using uniq
■ How to sort information in a file using sort
8
This hour begins by focusing on one aspect of constructing powerful custom commands in UNIX by using file redirection. The introduction of some filters, programs that are intended to be used as part of command pipes, follow. Next you learn another aspect of creating your own UNIX commands using pipelines.