Vous êtes sur la page 1sur 6

1 The Shell

The UNIX command interpreter or shell is the program you interact with when you log
into a Sun workstation and start a terminal window, or when you log in to a multiaccess
Sun UNIX system over the Internet via a terminal emulator such as telnet or
putty,
The default login shell for users in the Computer Science Department is the T-Cshell
(tcsh). It prompts you with a percent symbol (%) preceded by an identification string.
There are other shells available. They all have similar characteristics but each has
its own particular features. The T-Cshell is and extended version of the (csh) C-Shell
interpreter. This Note assumes you are using the T-Cshell.
The T-Cshell has the following features:
_ commands are invoked by naming them. Most UNIX commands are simply programs
which are run by the shell. For example,
scmabc-% ls
runs the program ls which reads your directory and lists the name of your files.
_ When you type a command name, the shell will search a set of directories until
it finds the program. This set is known as the search path.
The search path includes your current directory and one or two directories in
your home directory. You can write your own programs and invoke them automatically
(by naming them) from your current directory, or from subdirectories
“bin” or “solarisbin” in your home directory. The T-CShell keeps a hash table of
its path to speed-up access. You need to type the command rehash to update
the table if you write a new command and put it in “bin” or “solarisbin”.
_ commands often have argument strings which may, for instance, represent filenames.
E.g.
scmabc-% cp fileA fileB
is the copy command cp with two filename arguments; “fileA” is copied to a new
file, “fileB”.
Some commands have flag argument strings usually beginning with a “-”. The
flags modify the behaviour of the program being invoked:
scmabc-% ls -lt
makes ls give a long listing of the files sorted by time of creation.
CSG IN 2 (2001/02) 3
_ the shell will expand wildcards to match filenames in the current directory. For
example,
scmabc-% ls -l *.c
will give a directory listing of the files with names “something.c” (conventionally
C program source files).
_ most UNIX commands and programs adhere to a concept of standard input and
standard output. The standard input is a stream of data which the program reads
and the standard output is a stream of output written by the program. Often these
are both attached to the terminal so that input comes from your keyboard and
output goes to your screen. The shell lets you redirect the standard input and
output.
The symbol “
_
” is used to redirect standard input from a file and the symbol “
_

is used to redirect standard output to a file. For example:
scmabc-% cat
_
fileA
makes cat read from file “fileA”. It sends its standard output to the terminal or
screen.
scmabc-% cat
_
fileA
_
fileB
reads from “fileA” and writes to “fileB”.
_ the Shell has the facility to pipe the output of one program to the input of another.
The pipe symbol is “
_
”. For example:
scmabc-% ls
_
wc -w
pipes the output of ls (viz., your filenames) into the word count program wc.
The “-w” flag tells wc to count the number of words in its input. So the above
command counts the number of files in your directory.
_ You may assign aliases for commands or groups of commands:
scmabc-% alias xx exit
sets up an alias “xx” to stand for the command exit.
_ the shell has string and numeric valued variables.
scmabc-% set x = ”hello world”
scmabc-% echo $x
prints “hello world” on the screen. Some variables are pre-set, e.g. $home is
your home directory. Type set to see a list of assigned variables. The symbol
“˜” can also be used to refer to your home directory.
_ the T-Cshell is an interpretive programming language with while loops, foreach
loops, if-then-else statements, etc. See the Sun workstation on-line
documentation for more details.
_ scripts of shell commands can be written. These can be invoked in the same
way as compiled programs (i.e. just by naming them). For example:
scmabc-% cat
_
˜/bin/countfiles
#!/bin/csh
ls
_
wc -w
ˆD
scmabc-% chmod +x ˜/bin/countfiles
4 Computing Support Group: Introductory Note 2 (2001/02)
creates a C-Shell script file in your “bin” directory. The chmod command changes
its protection mode to make it executable.
The first line of the script tells UNIX that the script is written in the C-Shell language
(UNIX scripts can be written in any language), while the second line tells
the system to run the directory listing command, ls, and pipe its output to the
word count program, wc.
scmabc-% rehash
tells the shell to make a new table of the files on its search path and now
scmabc-% countfiles
will execute the script and output the number of files in your directory,
_ the shell has “job control”. Programs which don’t require any terminal interaction
can be run in the background.
scmabc-% sort bigfile
_
sortedfile &
scmabc-%
The “&” puts the sort program into the background and the Shell is available
immediately for other commands.
The special character “ˆZ” can be used to suspend a program which is running
in the foreground:
scmabc-% sort bigfile
_
sortedfile
scmabc-% ˆZ
Stopped
scmabc-%
You may now use bg to put the program into the background or fg to continue it
in the foreground. The command
scmabc-% jobs
lists the status of all stopped or background jobs along with a reference number
(1,2,3...). Use this number preceded by a “%” to make bg or fg act on a particular
job. If a backgound job needs terminal input, it will stop until you bring it into
the foreground.
_ the shell has a history mechanism - it remembers the last few commands.
scmabc-% history
lists the remembered commands along with a reference number. On a workstation,
you can cut and paste from the history to rerun a command. You can also
use the symbol “!” to rerun any command from the history:
scmabc-% !23
reruns command number “23” from the history.
scmabc-% !so
reruns the last command starting “so...”.
scmabc-% !!
reruns the last command.
See the manual page on the C-shell for more details (type man csh). The T-Cshell
has an additional mechanism which allows you to recall and edit previous commands
using the keyboard cursor keys. See the manual page on the T-Cshell (man tcsh) for
instructions.

Question 1: What is the major advantage of a hash table? (Asked by Silicon Magic
Corp. people)

Answer: The major advantage of a hash table is its speed. Because the hash function is to
take a range of key values and transform them into index values in such a way that the
key values are distributed randomly across all the indices of a hash table.

Question 2: What are the techniques that you use to handle the collisions in hash
tables?(Asked by Silicon Magic Corp. people)

Answer: We can use two major techniques to handle the collisions. They are open
addressing and separate chaining. In open addressing, data items that hash to a full array
cell are placed in another cell in the array. In separate chaining, each array element
consist of a linked list. All data items hashing to a given array index are inserted in that
list.

Question 3: In Unix OS, what is the file server? (Asked by Silicon Magic Corp.
people)

Answer: The file server is a machine that shares its disk storage and files with other
machines on the network.

Question 4: What is NFS? What is its job?(Asked by Silicon Magic Corp. people)

Answer: NFS stands for Network File System. NFS enables filesystems physically
residing on one computer system to be used by other computers in the network, appearing
to users on the remote host as just another local disk.

Question 5: What is CVS? List some useful CVS commands.(Asked by Silicon


Magic Corp.people)

Anser: CVS is Concurrent Version System. It is the front end to the RCS revision control
system which extends the notion of revision control from a collection of files in a single
directory to a hierarchical collection of directories consisting of revision controlled files.
These directories and files can be combined together to form a software release.
There are some useful commands that are being used very often. They are
cvs checkout
cvs update
cvs add
cvs remove
cvs commit

What is LILO?

LILO stands for Linux boot loader. It will load the MBR, master boot record, into the
memory, and tell the system which partition and hard drive to boot from.

What is the main advantage of creating links to a file instead of copies of the file?

A: The main advantage is not really that it saves disk space (though it does that too) but,
rather, that a change of permissions on the file is applied to all the link access points. The
link will show permissions of lrwxrwxrwx but that is for the link itself and not the access
to the file to which the link points. Thus if you want to change the permissions for a
command, such as su, you only have to do it on the original. With copies you have to find
all of the copies and change permission on each of the copies.

Write a command to find all of the files which have been accessed within the last 30
days.

find / -type f -atime -30 > December.files

This command will find all the files under root, which is ‘/’, with file type is file. ‘-atime
-30′ will give all the files accessed less than 30 days ago. And the output will put into a
file call December.files.

What is the most graceful way to get to run level single user mode?

A: The most graceful way is to use the command init s.


If you want to shut everything down before going to single user mode then do init 0 first
and from the ok prompt do a boot -s.

What does the following command line produce? Explain each aspect of this line.

$ (date ; ps -ef | awk ‘{print $1}’ | sort | uniq | wc


-l ) >> Activity.log

A: First let’s dissect the line: The date gives the date and time as the first command of the
line, this is followed by the a list of all running processes in long form with UIDs listed
first, this is the ps -ef. These are fed into the awk which filters out all but the UIDs; these
UIDs are piped into sort for no discernible reason and then onto uniq (now we see the
reason for the sort - uniq only works on sorted data - if the list is A, B, A, then A, B, A
will be the output of uniq, but if it’s A, A, B then A, B is the output) which produces only
one copy of each UID.

These UIDs are fed into wc -l which counts the lines - in this case the number of distinct
UIDs running processes on the system. Finally the results of these two commands, the
date and the wc -l, are appended to the file "Activity.log". Now to answer the question as
to what this command line produces. This writes the date and time into the file
Activity.log together with the number of distinct users who have processes running on the
system at that time. If the file already exists, then these items are appended to the file,
otherwise the file is created.

Vous aimerez peut-être aussi