User Tools

Site Tools


Sidebar

projects

pct0 (bonus; due 20230823)
wcp1 (due 20230823)
abc0 (due 20230830)
pct1 (bonus; due 20230830)
pct2 (due 20230830)
wcp2 (due 20230830)
dac0 (due 20230906)
pct3 (bonus; due 20230906)
wcp3 (due 20230906)
gtf0 (due 20230913)
pct4 (due 20230913)
wcp4 (due 20230913)
pct5 (bonus; due 20230920)
tpb0 (due 20230920)
wcp5 (due 20230920)
gfo0 (due 20230927)
pct6 (due 20230927)
tpb1 (due 20230927)
wcp6 (due 20230927)
pct7 (bonus; due 20231004)
tpb2 (due 20231004)
wcp7 (due 20231004)
bwp1 (bonus; due 20231018)
pct8 (due 20231018)
usr0 (due 20231018)
wcp8 (due 20231018)
fwg0 (due 20231025)
pct9 (bonus; due 20231025)
wcp9 (due 20231025)
gfo1 (due 20231101)
pctA (due 20231101)
upf0 (due 20231101)
wcpA (due 20231101)
pctB (bonus; due 20231108)
usf0 (due 20231108)
wcpB (due 20231108)
ldg0 (due 20231115)
pctC (due 20231115)
wcpC (due 20231115)
bwp2 (bonus; due 20231129)
pctD (bonus; due 20231129)
wcpD (bonus; due 20231129)
gfo2 (due 20231206)
pctE (bonus; due 20231206)
wcpE (bonus; due 20231206)
EoCE (due 20231214)
haas:fall2023:unix:labs:lab5

Corning Community College

CSCS1730 UNIX/Linux Fundamentals

Lab 0x5: More UNIX Shell Explorations

~~TOC~~

Objective

A continuation of our exploration of the UNIX shell and its capabilities.

Reading

In “UNIX for the Beginning Mage”, please read:

  • Chapter 6, pages 59-75.

In “Harley Hahn's Guide to UNIX and Linux”, please read:

  • Chapter 7 (“Using the Keyboard With UNIX”, pages 131-160).
  • Chapter 8 (“Programs to Use Right Away”, pages 161-188).
  • Chapter 10 (“Command Syntax”, pages 223-238).
  • Chapter 11 (“The Shell”, pages 239-254).
  • Chapter 12 (“Using the Shell: Variables and Options”, pages 255-276).
  • Chapter 13 (“Using the Shell: Commands and Customization”, pages 277-326).
  • Chapter 14 (“Using the Shell: Initialization Files”, pages 327-344).
  • Chapter 15 (“Standard I/O, Redirection, and Pipes”, pages 345-372).

In Learning the UNIX Operating System, 5th Edition, please read the following:

  • Chapter 5 (“Redirecting I/O”, page 87-96).

Wildcards

Wildcards allow for a means of referencing files shorthand- if you have several files with a commonality in their names, you can take advantage of that.

The common wildcards found in modern UNIX shells are:

Symbol Description
* match 0 or more characters
? match exactly one character
[ ] character class - match any of the enclosed characters.
[^ ] inverted character class - do not match any of the enclosed characters.

For example, take the following files:

file1 filea
file2 file1234
file3 fileZZZ
file4 file41
1. Do the following:
a.Create a shell/ subdirectory and cd into it.
b.What is your current working directory?
c.Using the touch(1) utility, create the 8 files mentioned above.
d.Show me how you created them.

Now that the files are created, let us try out some of these wildcards to see how they work.

2. Do the following:
a.Run an ls on your directory with an argument of file*
b.What happens?
c.Run an ls on your directory with an argument of file?
d.What files are listed? Why aren't the others?
e.Run an ls on your directory with an argument of file[23]
f.What files are listed? Why?
g.Run an ls on your directory with an argument of file[24a]*
h.What files are listed? Why?

In addition to a selection of symbols, you can also do ranges of values via the character class:

lab46:~/shell$ ls file[1-4]
  • For the lowercase alphabet: [a-z]
  • For the uppercase alphabet: [A-Z]
  • For all letters: [A-Za-z]
  • For digits: [0-9]
  • Or a subrange of digits: [4-9], [1-5]

I/O Redirection

Your terminal sends and receives data via three I/O streams: Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr).

Data that you enter via the keyboard is considered Standard Input. Output is send to your monitor (usually) and is considered Standard Output. Any errors that are generated are output via a different stream called Standard Error.

The shell provides features for manipulating these data streams, and this is called I/O Redirection. The characters used for redirection are:

Symbol Description
> STDOUT redirection operator
>> STDOUT append redirection operator
< STDIN redirection operator
2> STDERR redirection operator
2>> STDERR append redirection operator
| pipe

The pipe, as we will discover later, can be used to combine commands to create new functionality.

3. Do the following (be in your newly created shell/ subdirectory):
a.Using cat(1), display /etc/motd but redirect the STDOUT to a file called file1
b.View file1, what do you see?
c.Using echo, append the string “– This is text –“ to file1
d.View file1, what, if anything, has changed?

As we'll see, the functionality of “writing” vs. “appending” yields some drastically different results.

4. Continuing with our experiment:
a.Type: echo "More text ..." > file1
b.View file1, what happened? Why?
c.Type: ls file* | grep "file1" > file2
d.View file2, what happened? Why?

NOTE: grep is a utility used in text pattern matching.

Finally, redirecting I/O can be useful in filtering out unwanted output to the screen, be it error messages, or regular diagnostic messages. Here we will observe that there is a difference between STDOUT and STDERR.

5. At the prompt, do the following:
a.Type: ls file555
b.Do you get an error?
c.Type: ls file555 > /dev/null
d.Does the error go away? Why or why not?
e.Type: ls file555 2> /dev/null
f.Any difference? Why?
g.Type: cat < file2
h.What happened?

Pagers

A pager is a program that can be used to handle the output of a command- or to stop the scrolling to allow you to read the information- page by page or line by line. System V systems had a pager by the name of pg. More common pagers include the venerable more and less (note that less is more than more– if this statement is confusing, ask on the mailing list for an explanation).

Page 54 in Learning the UNIX Operating System, 5th Edition covers some basic usage of the less pager.

6. Create a command-line that does the following:
a.Get a long file listing of the entire /bin directory.
b.pipe the output through more or less.
c.Does it stop the output and let you read it?
d.What did you type?

Quotes

The UNIX shell utilizes the quotes in different ways, as identifed here:

Symbol Name Description
` backquote command expansion
' single (or full) quote literal quote, no expansion
" double (or partial) quote variable expansion

The backquotes are used in command substitution- it takes a command and will return the output of the command. The backquote is found on the ~ (tilde) key, just left of the 1 (and above tab) on QWERTY keyboards.

The single quotes will quote absolutely- all special meaning will be escaped. No substitution or expansion will take place.

Double quotes are less strict than the single quotes- they will allow variable names and commands to be expanded.

7. Do the following (be in your newly created shell/ subdirectory):
a.Type: echo $PATH
b.What happens?
c.Type: echo “$PATH”
d.Any changes? Why or why not?
e.Type: echo '$PATH'
f.What happens? Why?

We have been relying upon the $PATH environment variable throughout the course. It would behoove you to gain a better understanding of how it works, so that the operation of the system becomes more clear to you. Be sure to check out the referenced document “$PATH revisited” for an exposure to some of the concepts.

Exercise

Based on what you've learned from the lab and other available resources:

8. Explore the following:
a.What does the wc(1) utility do?
b.How do you get it to print just the number of lines?

Constructing command-line incantations is a must for any UNIX user/hacker. Since it is impossible to have a command to do everything, we must rely on our ability to combine smaller programs to create solutions. Note that the goal here isn't just busy work for this class– the ideal here is to promote laziness.. why type something 15 times when you could do it once by letting the computer do those steps for you? The easy way out is to learn how to make the computer to your work.

9. Do the following:
a.Using the cat(1) utility to output /etc/motd, pipe that output through wc and display the total number of lines in the file.
b.What was your command-line?
c.How many lines are there?

Wildcard patterns are important to get a handle on. These are one of the tools available at your disposal that will let the system start to perform work for you (instead of you working for the computer). Your master of them will go a long way.

10. Use the ls(1) utility in the /bin directory to display the following and tell me how you accomplished it:
a.All the files exactly four characters in length.
b.All the files that begin with any of your initials. How many are there?
c.All the files that begin with a, b, or c AND end with r, s, or t. How many? What are they?

Quoting plays an important part in our ability to make use of the system. It allows us to not only group information together, but it can also allow us to access data that the shell would otherwise interpret in a special way. Try your hand at the following:

11. WITHOUT USING A BACKSLASH, TAB COMPLETION, OR WILDCARD! Do the following:
a.In the UNIX Public Directory (shell/ subdirectory) is a file called “Long File $PATH for Shell Lab.text
b.Using your newfound quoting skills, display the contents of this file.
c.Tell me how you did it.

Conclusions

This assignment has activities which you should tend to- document/summarize knowledge learned on your Opus.

As always, the class mailing list and class IRC channel are available for assistance, but not answers.

haas/fall2023/unix/labs/lab5.txt · Last modified: 2014/02/17 11:58 by 127.0.0.1