Anda di halaman 1dari 6

Page 1 of 6

EE1030
Introduction to UNIX
Salt Lake Community College
Department of Engineering
(Electrical Engineering) MHM

ASSIGNMENT #1 OVERVIEW
SHELLS: The $ or % prompt that you see when you first log in is displayed by a special kind of
program called a shell. A shell is a program that acts as a middleman between you and the raw
UNIX operating system. It lets you run programs, build pipelines of processes, save output to
files, and run more than one program at the same time. A shell executes all of the commands that
you enter.
Each shell has its own programming language. One reasonable question to ask is: Why would
you write a program in a shell language rather than a language like C? The answer is that the
shell languages are tailored to manipulating files and processes in the UNIX system, which
makes them more convenient in many situations. In this section, the only shell facilities that we
use are the abilities to run utilities and to save the output of a process to a file. Let’s go ahead and
run a few simple UNIX utilities.

RUNNING A UTILITY: To run a utility, simply enter its name at the prompt and press the
Enter key. From now on, when I mention that you should enter a particular bit of text, I also
implicitly mean that you should press the Enter key after typing the text. Pressing the Enter key
tells UNIX that you’ve entered the command and that you wish it to be executed. Not all systems
have exactly the same utilities, so if a particular example doesn’t work, don’t be flustered. I’ll try
to point out the utilities that vary a lot from system to system. One utility that every system has is
called date, which displays the current date and time:

$ date ... run the date utility.


Whenever I introduce a new utility, I’ll write a small synopsis of its typical operation in the
format shown in the box below. The format is self-explanatory, as you can see
[sophist/home/smartpe]$ date
Mon Jun 9 17:10:39 MDT 2003
[sophist/home/smartpe]$

Try : date

Another useful utility is clear, which clears your screen.

Utility: clear

This utility clears your screen.

Try: clear (Comment in your document)


Page 2 of 6

OBTAINING ON-LINE HELP: MAN


There are bound to be many times when you’ re at your terminal and you can’ t quite remember
how to use a particular utility. Alternatively, you may know what a utility does, but don’ t
remember what it’ s called. You may also want to look up an argument that is not described in
this text or that differs slightly between different versions of UNIX. All UNIX systems have a
utility called man (short for manual page) that puts this information at your fingertips. man
works as follows:

Utility: man [-s section ] word


man -k keyword

The manual pages are on-line copies of the original UNIX documentation, which is usually
divided into eight sections. They contain information about utilities, system calls, file formats,
and shells. When man displays help about a given utility, it indicates in which section the entry
appears. The first usage of man displays the manual entry associated with word. If no section
number is specified, the first entry that it finds is displayed. The second usage of man displays a
list of all the manual entries that contain keyword.
The typical division of topics in manual page sections as follows:
1. Commands and Application Programs
2. System Calls
3. Library Functions
4. Special Files
5. File Formats
6. Games
7. Miscellaneous
8. System Administration Utilities

There is sometimes more than one manual entry for a particular word. For example, there is a
utility called chmod and a system call called chmod (), and there are manual pages for both (in
sections 1 and 2). By default, man displays the manual pages for the first entry that it finds, so it
will display the manual page for the chmod utility. In the case that other entries exist, the manual
page of the fist entry will state, “SEE ALSO...,” with a list of the other entries, followed by their
section numbers.
$man
[sophist/home/smartpe]$ man
What manual page do you want?
Try: man
$chmod
CHMOD(1) FreeBSD General Commands Manual CHMOD(1)

NAME
chmod - change file modes

SYNOPSIS
chmod [-fv -R [-H | -L | -P]] mode file ...
Page 3 of 6

DESCRIPTION
The chmod utility modifies the file mode bits of the listed files as
specified by the mode operand.

The options are as follows:

-H If the -R option is specified, symbolic links on the command line


are followed. (Symbolic links encountered in the tree traversal
are not followed by default.)

-L If the -R option is specified, all symbolic links are followed.

-P If the -R option is specified, no symbolic links are followed.


This is the default.

If you want to look at the rest of the screen use Enter or page down. To end use Control c
(hold control press c )

Terminating A Process: Control-C


There are often times when you run a program and then wish to stop it before it’ s finished. The
standard way to execute this action in UNIX is to press the keyboard sequence Control-C.
Although there are a few programs that are immune to this form of process termination, most
processes are immediately killed and your shell prompt is returned. Here’ s an example of the use
of Control-C:

$ man chmod
CHMOD (IV) USER COMMANDS CHMOD (IV)
NAME
chmod - change the permissions mode of a file
SYNOPSIS
^C ...terminate the job and go back to the shell.(^ means holding the control key)
$_
Try: ^c ( Comment in your document)

Pausing Output: Conrol-S/Control-Q


If the output of a process starts to rapidly scroll up the screen, you may pause it by pressing
Control-S. To resume the output, you may either press Control-S again or press Control-Q. This
sequence of control characters is sometimes called XON/XOFF protocol.

End of Input: Control-D


Many UNIX utilities may take their input from either a file or the keyboard. If you instruct a
utility to do the latter, you must tell the utility when the input from the keyboard is finished. To
do so, press Control-D on a line of its own after the last line of input. Control-D signifies the end
of input.
Page 4 of 6

Try: ^d ( Comment in your document)

Login again

CREATING A FILE

Utility: cat -n {fileName}*

The cat utility takes its input from standard input or from a list of files and displays them to
standard output. The -n option adds line numbers to the output. cat is short for “catenate” which
means “to connect in a series of links.”
Try cat smartpe_a
now
Try: cat >smartpe_a ( Remember you should use your login name ,underscore, a)
( type about two pages of anything even junk)
use ^c or ^d to end the writing.
(Do not copy in your document, just write comment)
Now
Try: cat smartpe_a
(Do not copy in your document, just write comment)

LISTING THE CONTENTS OF A DIRECTORY: ls( this is LS, but use small letters)
To confirm the existence of the above file in home directory and see how many bytes of storage is
used, use the ls utility, which lists information about a file or a directory. ls works like this:

Utility: ls -adglsFR {fileName}*{directoryName}*

With no arguments at all, ls list all of the files in the current working directory in alphabetical
order, excluding files whose names start with a period. The -a option causes such files to be
included in the listing. Files that begin with a period are sometimes known as hidden files. The -d
option causes the details of the directories themselves to be listed, rather than their contents. The -
g option lists a file’ s group. The -1 option generates a long listing, including permission flags, the
file’ s owner, and the last modification time. The -s option causes the number of disk blocks that
the file occupies to be included in the listing. (A block is typically between 512 and 4K bytes.)
The -F option causes a character to be placed after the file’ s name to indicate the type of the file: *
means an executable file,/ means a directory file, @ means a symbolic link, and = means a socket.
The -R option recursively lists the contents of a directory and its subdirectories. To obtain a listing
of directories other than the current directory, place their names after the options. To obtain
listings of specific files, place their names after the options.

Some of the ls options described above won’ t mean a lot right now, but will become increasingly
relevant as this book progresses.

Here’ s a brief overview ls:


Page 5 of 6

Field # Field Value Meaning

1 -rw-r--r-- the type and permission mode of the file, which


indicates who can read, write, and execute the file

2 1 the hard-link count (discussed in Chapter 7)

3 glass the username of the owner of the file

4 106 the size of the file, in bytes

5 Jan 30 19:46 the time that the file was last modified

6 heart the name of the file

You may obtain even more information by using additional options:


The -s option generates an extra first field, which tells you how many disk blocks the file
occupies.

Try: ls

LISTING A FILE: cat, more, page, head, and tail


To check the contents of the file that you had created in my home directory “ /home/smartpe” , you
can list its contents to the screen using the cat utility.
cat can actually take any number of files as arguments, in which case the contents of the files are
listed together, one following the other. cat is good for listing the contents of small files, but it
doesn’ t pause between full screens of output. The more and page utilities are better suited for
listing the contents of larger files and contain advanced facilities such as the ability to scroll
backward through the listing of a file. Here are some notes on each utility for listing the contents
of a file:

Utility: more -f [+lineNumber] {fileName}*

The more utility allows you to scroll through a list of files, one page at a time. By default, each
file is displayed starting at line 1, although the + option may be used to specify the starting line
number. The -f option tells more not to fold (or wrap) long lines. After each page is displayed,
more displays the message “ –More–“ to indicate that it’ s waiting for a command. To list the next
page, press the space bar. To list the next line, press the Enter key. To quit from more, press the
“ q” key. To obtain help on the multitude of other commands you could issue at this point, press
the “ h” key.
Try: more (Comment in your document)

Utility: page -f [+lineNumber] {fileName]*

The page utility works just like more, except that it clears the screen before displaying each page.
This feature sometimes makes the listing a little quicker.

While we’ re on the topic of listing files, there are a couple of handy utilities called head and tail
Page 6 of 6

that allow you to peek at the start and end of a file, respectively. Here’ s how they work:

Utility: head -n {fileName}*

The head utility displays the first n lines of a file. If n is not specified, it defaults to 10. If more
than one file is specified, a small header identifying each file is displayed before its contents.

Utility: tail -n {fileName}*

The tail utility displays the last n lines of a file. If n is not specified, it defaults to 10. If more than
one more is specified, a small header identifying each file is displayed before its contents.

DELETING A FILE: rm

Utility: rm -fir [fileName}*

The rm utility allows you to remove a file’ s label from the hierarchy. When no more labels
reference a file, UNIX removes the file itself. In most cases, every file has only one label, so the
act of removing the label causes the file’ s physical contents to be deallocated.
The rm utility removes a file’ s label from the directory hierarchy. If the filename doesn’ t exist, an
error message is displayed. The -i option prompts the user for confirmation before deleting a
filename; press y to confirm and n otherwise. If fineName is a directory, the -r option causes all of
its contents, including subdirectories, to be recursively deleted. The -f option inhibits all error
messages and prompts.

Try: cat>smartpe_a_2 (Just write a few sentences)


Try: ls
Try: rm smartpe_a_2 (Comment in your document)

PRINTING A FILE: lp, lpstat, and cancel

The UNIX print utility called lp, which works like this:

Utility: Ip [-d destination] [-n copies] {fileName}*

Ip prints the named file(s) to the printer specified by the -d option. If no files are specified,
standard input is printed instead. By default, one copy of each file is printed, although this default
may be overridden by using the -n option to specify the number of copies.

Try: lp smartpe_a ( your printed document should be in room SI075)

Anda mungkin juga menyukai