ctx->lectures->lecture1

Meeting the $SHELL

What is a shell anyway?

Well a shell is just a program, but a very special one... It is the primary method of interfacing a computer with you, the user. Let's think for a moment an analogue in everyday life... Imagine a car. That's a hell of complicated machine. It has all sorts of stuff in it. But an everyday driver, only needs some parts of it in order to drive . These are the wheel, the pedals, the gearshift and the display. Using only these four, you harness the full power of a modern car. This common setup, completely hides the differences between each car. Therefore it is a method of INTERFACING. You get that face in every car without having to know about engine details, manufacturer tricks and dead bodies in the trunk ;). Well a shell acts in a similar way. It presents you with an input field where you can enter the names of commands to be run (programs) and commands to the shell (the running program). This second option, implies the art of SHELL SCRIPTING. The method of programming the way the shell behaves.

Types of shells

If the shell is such an important program, why haven't you seen one in Windows? Well you have. Based on our definition "Windows Explorer" is kind of a shell. Meaning it let's you execute basic operations (copy, delete, rename) on files. But it would be a shame to call this idiotic thing a shell. So let's move on to some real ones ;). UNIX usually has many shells to offer to the user, letting him choose the one that he prefers. But for everyday usage, all act with the same UNIXy way. They execute commands, redirect I/O (input/output) and are programmable. The most widely used shells in UNIX currently are:

To find out what kind of shell you are using in your UNIX system, you can run:

% env | grep SHELL

Redirection

Most processes initiated by UNIX commands write to the standard output (the terminal screen) and many take their input from the standard input (the keyboard). There is also the standard error where processes write their error messages, by default, the terminal screen. Remember the cat command that writes the contents of a file to the screen. cat myfile prints the contents of myfile. Now write the cat command without any argument. Then type a few words on the keyboard and press the Return key. Finally hold the Ctrl key down and press d (written as ^D for short) to end the input. What has happened? If you run the cat command without specifying a file to read, it reads the standard input (the keyboard), and on receiving the "end of file" (^D), copies it to the standard output (the screen).

In UNIX, we can redirect both the input and the output of commands. For output redirection we use the > operator. For example, to create a file called animalist1 containing a list of animals type:

% cat > animalist1
chicken
cow 
moose
^D

Press Return after each animal name (Ctrl D to stop).

What happens is the cat command reads the standard input (the keyboard) and the > redirects cat's output, which normally goes to the screen, into a file called animalist1. To read it's contents type:

% cat animalist1

For input redirection we use the < operator. For example take the command sort which alphabetically or numerically sorts a list. Type:

% sort
goths
vikings
windows users
visigoths
ostrogoths
^D

Type in the names of some barbaric tribes. Press Return after each one. The output will be:

goths
ostrogoths
vikings
visigoths
windows users

As usual, Windows users are the lowest of them all, even alphabetically. Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of barbarians, that you now have in a file called barblist type:

% sort < barblist

The sorted list will be output to the screen. To output the sorted list to a file, type:

% sort < barblist > barblist-sorted

The final ace up our sleeves is the | symbol (PIPE). This takes the output of one command and sends it as an input to the other. Examples:

Print the number of files in the current directory.

% ls | wc -l

Print those file names containing the string old.

% ls | grep old
A taste of things to come

Write the following script into a file (name it script.sh), then run chmod +x ./script.sh. This gives to the file script.sh permission to be executed (man chmod for info). ./script.sh runs the script (notice the ./).

#!/bin/sh
count=0
for file in `ls`
do
	count=`expr $count + 1`
	echo "$count: $file" > menu_file
done
echo "Please select a number from this menu"
cat menu_file
read choice
echo "Thanks you chose $choice"
filename=`grep $choice menu_file | cut -f2 -d:`
echo "You chose $filename"

Observe this script. Can you understand what it does? Try to change the line echo "$count: $file" > menu_file to echo "$count: $file" >> menu_file. Then run it multiple times... Can you see the bug? ;)

Finally, some useful links:

Until next time... ;)
Happy Hacking...

dsp@