Harley Hahn's Guide to
|
A Personal Note
Chapters...
Command
INSTRUCTOR |
Exercises and Answers for Chapter 21... Displaying Files Review Question #1: Which programs do you use to display:
• A text file one screenful at a time
Answer Use the following program to display...
Review Question #2: As you are using less to display a file, which commands do you use to perform the following actions?
• Go forward one screenful
Answer
Review Question #3: You can use less to display more than one file, for example: less file1 file2 file3 file4 file5 As you are reading, which commands do you use to perform the following actions?
• Change to the next file
Which commands do you use to search forward and search backward within all the files? Answer
Review Question #4: What command would you use to watch the end of a growing file? Answer tail -f Review Question #5: When you display a binary file, what is canonical format? How do you display a file in canonical format using hexdump? Using od? Answer When displaying or printing binary data, canonical format consists of 16 bytes per line such that: • To the left of each line is the offset in hexadecimal • In the middle are the actual bytes, also in hexadecimal • On the right are the ASCII equivalents To display a binary file in canonical format use either:
hexdump -C
Applying Your Knowledge #1: Check the value of your PAGER environment variable. If it is not set to less, do so now. Display the man page (Chapter 9) for the less program itself. Perform the following operations:
• Display help information
• Search forward for "help"
• Go to the end of the man page
• Quit Answer To check the value of your PAGER environment variable: echo $PAGER To set PAGER to less for the Bourne Shell family (Bash, Korn Shell): export PAGER=less To set PAGER to less for the C- Shell family (C-Shell, Tcsh): setenv PAGER less To display the man page for less: man less To perform the following operations:
Applying Your Knowledge #2: You want to use less to display the contents of the file list, which contains many lines of text. Along with the text, you also want to see line numbers. However, there are no line numbers within the text, and you do not want to change the original file in any way. How would you do this using nl and less? How would you do it using only less? Is there any advantage to using nl? Answer Using nl and less: nl list | less Using only less: less -N list The advantage to nl is that you can use options to control the numbering. You cannot do this with less -N. Applying Your Knowledge #3: Convert the following binary (base 2) number to octal (base 8), hexadecimal (base 16), and decimal (base 10): 1111101000100101 You must show your work. Answer base 2 = 1111101000100101 base 8 = 1 111 101 000 100 101 = 175045 base 16 = 1111 1010 0010 0101 = FA25
base 10 = (1*8^5) + (7*8^4) + (5*8^3) + (0*8^2) + (4*8^1) + (5*8*0)
Applying Your Knowledge #4: Use the strings command (Chapter 19) to look for character strings within the binary file /bin/ls. Then select one string and use hexdump or od to find the exact position of that string in the file. Answer To look for character strings within /bin/ls: strings /bin/ls For Further Thought #1: You want to use less to search through five different files, looking for a particular sequence of words. The obvious solution is to use less as follows: less file1 file2 file3 file4 file5 However, this requires you to keep track of and manipulate five different files. Instead you might first combine the files into one large file: cat file1 file2 file3 file4 file5 | less Will this make your job easier or harder? Why? Answer Combining the files will make your job harder, because when you find what you want, you won't know which file it was in. The easiest way to do the job is to start less with five different files, and use the /* command to search all the files for the particular sequence of words you want to find. For Further Thought #2: In the text, we discussed four numbers systems: decimal (base 10), binary (base 2), octal (base 8) and hexadecimal (base 16). In principle, any positive whole number can be used as a base. Consider base 12, also called DUODECIMAL. In base 12 we use the digits 0 through 9. For the extra two digits, we use A and B. Thus, in base 12, we count as follows: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, 10, 11, 12, and so on. The number 22 in base 10 equals 1A in base 12. What important advantages does base 12 have over base 10? Hint: How many factors does 12 have compared to 10? How does this simplify calculations? Would our culture be better off if we used base 12 instead of base 10? In spite of its advantages, we don't use base 12 with computers. Most of the time we use base 16, which is much more complicated. Why is this? By the way, if you have ever read the Lord of the Rings books by J.R.R. Tolkein, you will be interested to know that the Elvish languages use a duodecimal number system. Answer The number 12 has 6 factors (1, 2, 3, 4, 6, 12). The number 10 has only 4 factors (1, 2, 5, 10). Because base 12 has more factors, calculations are easier, especially division. Also, more values can be expressed as simple fractions. For example, there are 12 items in a "dozen". Consider the following:
1 = 1/12 of a dozen
Now, let's make up a unit called "ten" that contains 10 items. Consider:
1 = 1/10 of a ten
(Take a moment to think about this.) Our culture would be better off using base 12 than base 10, as calculations, especially those using division and fractions, would be easier. Note that there are:
12 items in a dozen
The troy ounce and troy pound, were medieval measurements, widely used at one time. The name "troy" comes from the city of Troyes, France, an important mediaeval trading center. Today, troy units are used only to measure precious metals, gemstones and gun powder. A troy ounce is defined as 480 (4x12) grains. A troy pound is defined as 12 troy ounces. The ancient Romans used a fraction system based on 12. Within this system, the word uncia was used to denote a type of coin and a unit of length. This word became the ancestor of both the English words "ounce" and "inch". We use base 16 with computers even though it is more complex than either base 10 or 12. This is because (as explained in the text) base 16 can be thought of as a compact way to express base 2, and base 2 represents the off/on system fundamental to computer data storage. Exercises: Introduction | Chapter list
© All contents Copyright 2024, Harley Hahn
|