Teach Yourself UNIX Shell Programming in 14 Days

Chapter 1: Introduction to the Shell

 

Objectives:

This chapter introduces UNIX concepts such as the history of the operating system, logging into a system, setting passwords, introduction to shells, and using shells to issue commands to the operating system. The objectives important to this chapter are to:

  • Introduce UNIX concepts
  • How to log in
  • Difference between programs, shell commands and scripts
  • Shells and shell types
  • Use of shells
  • Switching shells
  • UNIX commands: ls, wc, passwd, who, echo, pwd
  • Logging off the system

 

Concepts:

Note: this book has several errors in it, mainly having to do with screen examples not matching the text. Experiment with any concept that is not familiar to you and with all concepts that are inconsistent (taking care not to crash the system.) Why are we using such a book? I am reminded of Winston Churchill's comment about democracy, that it is a very poor form of government, however, all the others are so much worse.

UNIX is an operating system that, like the C language, was first developed at Bell Labs. There are many versions of UNIX available now. The versions can usually be divided into two major groups: those closely related to the current version of Bell UNIX (SVR4) and those closely related to Berkeley UNIX (BSD), a version developed at the University of California at Berkeley.

There are three major parts to UNIX itself: the kernel, the file system, and the shell. The kernel manages system resources, the file system is a hierarchical tree system, and the shell is the command interpreter.

To log in to a UNIX system, you need an operator id (user name), and a password. Expect to see the word login somewhere in the prompt that lets you log in. Passwords are typically masked when you enter them, so they cannot be seen on the screen. This does not prevent someone from watching your fingers as you type, however, so don't presume that no one knows your password.

Once you are logged in, the system presents you with a prompt or a menu, depending on your user profile. The prompt may be a dollar sign, or a percent sign, depending on which shell you use.

You interact with the system through your current shell, a program that intercepts your keyboard input, translates your input to binary code and communicates that translated input to the system. The system communicates back to you in the same way, binary code to the shell, translation in the shell, and output to you.

Programs on a UNIX system are typically compiled. Compiling translates the source code to binary code and lets the system read and execute the programs directly.

Commands passed to the system through the shell are interpreted by the shell, turned into binary code and passed to the system. This is necessarily slower.

Shell scripts are text files that contain one or more shell commands, which are interpreted by the shell, turned into binary, and passed to the system. It is necessary to decide whether a given system task is better handled by a slower, easily modified script, or by a faster, compiled program.

A shell has several basic features:

  • Command Execution - it accepts commands and passes them to the system
  • Filename Substitution - shell commands can accept wild cards to execute across multiple files
  • I/O Redirection and Pipeline Hookup - shell commands can send programs input from sources other than the standard input device, and cause output to go to non-standard places as well. Sending output from one program into another is piping.
  • Environment Control - customizing where files are stored, setting environment variables
  • Interpretive Programming Language - variables may be used in the shell

There are several shells commonly available:

  • Bourne shell (sh) - most common, usually available
  • C shell (csh) - syntax similar to C, usually BSD related, and scripts not compatible with sh
  • Korn shell (ksh) - several variants, it can run sh scripts
  • Bourne-again-shell (bash) - freeware, no support
Shells can operate in interactive or non-interactive modes. Interactive mode means taking input from the user while running. The command
read src
means to take input from the user and store it in the variable src. The user's input is stored as a string.

# is a comment marker in shell scripts. The shell ignores text on a script line after a #.

A useful command that will enable the use of the backspace key is

stty erase ^H
Generate the ^H by holding down the control key and generating a capital H.

The Ls command in UNIX is frequently used to list the contents of the current directory.

Ls -al            #  means to list with all details

The echo command puts text on the screen.

echo "this is inside quotes to maintain this     space"
 #  without quotes, spaces truncate

The accent grave character looks like a back-apostrophe. It forces execution of the program named inside a pair of such characters.

x=`pwd`  
#  means execute the pwd command and assign the result to variable x

pwd is a command that reports the path to the current directory

To determine which shell you are in, try typing the command

echo $SHELL
The response should tell you which program (sh, csh, ksh or bash) was run to start the shell.

You can switch from one shell to another, but only if the second one exists on your system. This will eat up memory, since it creates another shell session on top of the one you are running. The exit command takes you back one level to the shell you were in. It will log you out if you are not in a second shell. Control-d will also log you out of the current shell.

You may wish to enter more than one command per line. Use semi-colons to separate the commands, or the second and subsequent commands will be taken as parameters of the first.

The passwd command may be used to change your password. Be careful in doing so. It is usually a pain to get the system administrator of a busy system to take time to give you a new one when you forget what you changed it to.

While it is possible in theory to halt the flow of information across your screen with the control-s command, it is neither practical nor recommended to do so. Use of the more command is preferred.

Exit the system with the exit command if possible, and with control-d if not.