next up previous
Next: About this document Up: No Title Previous: No Title

A UNIX Warmup

(Not to hand in, but try to do by Wednesday 1/13. UNIX will also be covered in discussion section.)

  1. Login to both your NT and UNIX accounts and reset passwords, according to instructions in the lab.

    To access your UNIX account from the lab NT network, you can run the XWIN32 program, which may on the desktop or in the Internet folder or on the Start menu under Internet. This emulates X-Windows, which is the windowing system used with UNIX, so it's just as though you were using a UNIX workstation. The windows work somewhat like those in Windows NT. Most important is that the cursor should be in the window in which you intend to type. To telnet from outside, you can use the hostname hermosa.pic.ucla.edu [We need to double-check this; if it doesn't work check the PIC 40 home page for updated information.]

  2. There is an on-line dictionary file that we'll sometimes use for examples. Look at some of it using the command

    more /usr/dict/words (and press the RETURN or ENTER key, depending on what your keyboard has)

    Now press the space bar to see a page at a time (``more and more'' of the file), or the d key to see half a page at a time. You can use q to quit.

  3. ``Folders'' in Windows terminology are ``directories'' in UNIX, and ``shortcuts'' are ``symbolic links''. Also, path names have forward slashes in UNIX, but backslashes in Windows.

    There is a directory in which course materials will sometimes be provided, but it has a complicated name. Make a simpler name for it, h in your own home directory, by creating a symbolic link:

    ln -s /usr/class/handout/class/pic40.1 h

    (This is called the ``class handout'' directory, but it's for providing files, not for regular kinds of handouts.) Try these:

    ls (list the current directory)

    ls h (list the contents of h )

    more h/wds

    cd h (change the current directory to h )

    more wds

    pwd (print working directory--in other words, where am I?)

    cd (go to your home directory)

    Note: Path names starting with a slash are ``full path names'', usually not in your personal ``directory tree''. Path names starting without a slash are assumed to start in the ``current directory''. Your own home directory can always be called ~ ; for example, later on when you have lots of directories if you're in one you could say

    cd ~/h (go to the subdirectory h of your home directory)

    Another note: cd .. goes up a directory, but if the directory is a symbolic link it may go up to one you didn't intend.

  4. There is a ``word count'' program that counts lines, individual words, and characters. Try it:

    wc /usr/dict/words

    (Why are the first two of the three numbers the same?) If you want only the line count, use

    wc -l /usr/dict/words

  5. Many programs have a ``manual page''. Try

    man wc ( man is controlled like more )

    In UNIX a command may be followed by ``arguments'', some of which may be file names. Others, with minus signs, are ``switches'' or ``options'' that tell the command to do something special. The manual page lists optional arguments in brackets [] and then explains any options below.

    Later, in Perl, you'll learn how to write programs that have a name that you choose and that grab the arguments, which are simply strings, and interpret them any way you want. This can also be done in C++.

  6. There is a line-selection program grep (for ``get regular expression''). To list all lines containing a substring ``boo'', use

    grep boo /usr/dict/words

    If you're at home connected via a phone line, be careful not to try a selection that prints out more than the line can handle. If you do start getting too much output from a program, stop it with ^C (control C), meaning to hold the CRTL key down like a shift key and press c . Do not type ^ !

    You can ``pipe'' the output of one program as the input to another by using a vertical bar:

    grep boo /usr/dict/words | wc -l

    What do you get? Try searches with some other substrings.

  7. To find all words that contain ``boo'' but not ``book'', you can first look at man grep to find an option for grep that makes it select lines that do not have the pattern. (Don't be intimidated by all the extraneous information.) I'll call this option -??? since you're supposed to find it. Now do

    grep boo /usr/dict/words | grep -??? book (Again, don't type these question marks!)

    How would you have the computer count the answers?

  8. There is a program to sort lines of a file:

    sort h/wds

  9. The programs wc , grep , and sort first look to see if one or more filename arguments have been provided. If no filename has been provided, they try to get information from the ``standard input''. You as the user determine where the standard input comes from by what you type on the command line. The standard input can be from a named file, a pipe, or from the keyboard. Try these:

    sort < h/wds (standard input from a file)

    grep ob < h/wds | sort ( grep from a file, sort from a pipe)

    sort (standard input from the keyboard)

    In this last one, the computer will stop dead waiting for you to type something. So type several lines, each ending with a RETURN (or ENTER, depending on your keyboard labels). Then type ^D as the newline marker (meaning to hold the CTRL key down and press d ). You should get your lines back, sorted.

  10. Correspondingly, these programs are writing their output to the ``standard output'', and you determine where that goes. You have already done these:

    sort < h/wds (standard output to the screen)

    grep ob < h/wds | sort (standard output of grep is piped to standard input of sort )

    Now try this:

    sort < h/wds > tmp (standard output to a file named tmp )

    Use more to view the file tmp to see if this worked. When you're through, remove tmp by the command

    rm tmp

  11. wc , grep , and sort are examples of ``filter'' programs--programs that can take information from the standard input, process it, and write to the standard output. In contrast, rm and ls are not filter programs. We'll discuss how to write filter programs with perl .

    On the other hand, these programs also can be used with filename arguments, even more than one. Try

    wc /usr/dict/words h/wds

  12. Now, for email on this system, you have to decide whether to receive it here (and read it with a mail program here or on the NT system) or to forward it to your preferred location. For the NT system, there is nothing to do.
  13. A command that is sometimes useful is echo , which simply takes its arguments and copies them to the standard output. Try

    echo hi there

    echo hi there | wc

  14. To forward, you need to make a file .forward with only one line, containing your preferred email address. (Files whose names start with a dot are not normally listed by ls , so they're ``out of the way''.) You could do this with an editor, but it's simplest to do this:

    echo lee@ucla.edu > .forward (if that's your address)

    Check the file with more

  15. Useful commands not covered already are

    cp myfile1 myfile2 (copies an existing file to a second file, creating the second file if it doesn't exist, or overwriting if it does; you may be asked to confirm that you want an overwrite)

    mv myfile1 myfile2 (moves an existing file to a second file, the same thing as renaming)

    lpr myfile (send a file to the printer)


next up previous
Next: About this document Up: No Title Previous: No Title

Kirby A. Baker
Tue Jan 12 19:15:11 PST 1999