Notes 2
Home Up Stream A Stream B Shell Skills RegExp Skills Perl Skills Resources Using FTP Web Logs Permissions Notes 1 Notes 2 Inodes Unix exploit - PATH

 

Notes 2

The Unix Shell(s)

A Unix shell is a program that looks for a program and, if it finds it, runs it. (Yes, a shell can run another shell, and that shell can run another shell, and so on!)   Shells are not privileged programs; anyone can write a program that does what a shell does.

The shells usually wait until the running program finishes before they will prompt you for another program to run.  You can tell the shell not to wait; it can start the program in the background and immediately prompt you for another line of input.   With some shells, you can even change your mind about a program for which the shell is waiting and you can send the program into the background even after it has started running.

The shells usually issue a prompt to indicate that they are waiting for you to type something.  (You can turn off the prompt or change it to be anything you prefer.)  The line of words that you type in response to the prompt is called a command line.

Usually, the program to be run appears at the beginning of the command line.  We call the name of the program being run the command name.  The command name is separated from the rest of the command line by a space or tab, e.g.

echo "This line of text echoes to the screen."
ping -s localhost
ls -l
rm *.bad
date

The rest of the words after the command name are called command arguments.   Each argument is separated from its neighbours by spaces or tabs.  Most shells ignore extra spaces around arguments, so one space or ten spaces has the same effect.

Some of the arguments may modify the way the command works; these arguments usually start with a special character such as a dash, e.g. "-s" or "-l".   These are called options or switches.  Other arguments are things to be acted upon by the command, e.g. the rm command takes file names as arguments and removes those files:   rm file1 file2

STTY and special characters

The Unix stty command lets you change which characters perform "editing" functions as you type, such as backspacing or erasing the entire line.

You can use the stty command to turn any character into an editing character.  Doing so with characters that you commonly use will cause you problems.   For example, if you decided to turn the character "e" into a backspace character, you would never be able to type a word that contained a letter "e", since every time you pressed the "e" key it would backspace and delete the previous character.

Most special characters are selected to be control characters - characters created by holding down the control key and pressing some other key at the same time.  Since these special characters seldom appear in command lines and other text, they are safe to use for special editing functions.

Because control characters are not like letters or numbers, they do not print on your screen.  They are called non-printing or unprintable characters.   Still, they are real characters.  To make them more visible as you type them or display them, many Unix commands let you type in two printing characters to represent a control character.  Many programs will display the control characters using the same two-printing-character convention.  (If the program didn't do this, you wouldn't see anything on your screen, because control characters are unprintable!)

stty erase '^h'
stty intr '^?'
stty

The first character in the pair that represents a control character is the circumflex (sometimes called the "up-arrow" character).  The second character is the character indicating which control character you want to represent.  When given as an argument to the stty command, '^h' would be interpreted as control-h.  The stty command displays control characters using the same conventions.

Though the DELETE character is not strictly a control character (you cannot send a delete character by using the control key and another key), it is an unprintable character.  It is also given a two-character sequence to make it more visible: '^?'.  (You cannot send a delete by typing control-?.)

Getting Help (man pages)

The Unix manual has many sections.  They are often numbered 1 through 8; but, various Unix vendors add their own sections and some sections have sub-sections, e.g. "1c", "8m", etc.  When you use the "-k" (keyword) search option to the man command, you obtain one-line descriptions from manual pages in all the sections of the Unix manual, even sections that aren't of interest to you.  If you are only interested in Unix command names, only the lines from section 1 of the manual will be relevant.

Here is a trick for selecting just the descriptions that are in section '(1)' of the manual.  As an example, here is a command line that finds only Unix commands that are related to passwords:

man -k password | grep '(1)'

This example uses the pipe symbol (sometimes called a bar or OR-bar) to connect the output of the man command to the input of the Unix grep command.  The grep command selects only those lines that have the pattern '(1)' in them.

Paginating output: more

Sometimes output from a program is so long that it doesn't fit on the screen.   (Many man pages are like this.)  The more command is designed to display output one screen at a time.  When you see the More? prompt at the bottom of a page of output, you have many options (read the man page for full details).  The common options are:

type a space to advance to the next page of output
use the Enter or Return keys to advance one single line at a time
type the letter   b   to go backwards
type the letter   q   to quit paginating the file
interrupt more by using your interrupt character (usually ^c or ^?)

You can also search for text and do other wonderful things.  Read the man page for details.

Unix commands

Unix commands discussed in class:

/usr/sbin/ifconfig: find the IP number of your machine, and its broadcast address
echo: echo (display, print) text on your terminal
alias: create a synonym (alias) for a command, e.g.
    alias manual="man"
    alias password="passwd"
    alias ifconfig="/sbin/ifconfig"
    alias happy="echo I am a happy person."
    alias ping="/usr/sbin/ping"
ping: ask a machine to respond to you; requires the "-s" option on Solaris
telnet: connect a terminal emulator to a remote computer (also available on Windows 9x)
grep: find lines that match a pattern
man: search the manual; the "-k" option searches man page titles by keyword

Different versions of Unix have enhanced and altered the behaviour of various Unix commands.  Often, the behaviour of the command will be basically the same; however, the names and meanings of the options may vary.

Note how the behaviour of the ping command differs slightly between Linux and Solaris.  On Linux, the command issues repeated ping's every second; you must interrupt the command to make it stop.  On Solaris, you require a "-s" option to the command to make it behave this way, otherwise is simply tells you if the host is up or not responding.