Understanding the Basics of Shell Programming

EXPERIMENT NO. 9

Understanding the Basics of Shell Programming

Objectives:

  1. To enhance programming skills using shell programming
  2. To investigate the use of shell commands in shell programming
  3. To exploit the full power of UNIX by creating and executing shell programs

Overview

Overview

One of the major components of the UNIX operating system is the UNIX shell, the interface between the user and the operating system itself. The UNIX shell is also equipped with a powerful programming language that helps to exploit the full power of UNIX.

In this section, you will learn to create and execute shell programs (also known as shell scripts or shell procedures) and use some of the basic shell programming features.

Shells

Different Flavors of UNIX Shells

In UNIX, “shell” refers to a program, usually written in the C programming language that serves as the link between the user and the UNIX operating system. The shell’s function is to accept, interpret, and execute commands.

Throughout the history of UNIX, various shells have been written by different system programmers; and some became popular. Though written by different programmers, two features are common: they provide a way to enter commands using the command line interface and provide a programming language for writing shell programs.

On most systems, different shells are available to choose from. Each user can select he shell that best suits his or her need and even switch back and forth. Some of these shells are:

  1. The Bourne Shell. The original UNIX shell, created by Steve Bourne at AT&T’s Bell Laboratories. It is considered as the “standard” UNIX shell.
  2. The C Shell. In the mid of 1970’s, Bill Joy at University of California at Berkeley (UCB), wrote a shell for UNIX that would accomplish the basic task of a shell but with different features and capabilities different from that of the Bourne shell.
  3. The C-shell prompt is a percent sign (%), rather than the dollar sign ($) used by the Bourne shell.

  4. The Korn Shell. In the early 1980’s David Korn at Bell Labs produced what many perceived as a perfect shell: the Korn shell (Or “K” shell). It is “backward compatible“ with the Bourne shell, including its programming language.
  5. In addition, the Korn shell added most of the C shell’s enhancement and a few of its own. Aliases are borrowed from the C shell, along with a more powerful, yet easier-to-use history mechanism.

  6. The Bourne-again Shell (Bash). Bash incorporates useful features from the Korn and C shells (ksh and csh). It is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE standard 1003.1).

Your Shell

Which Shells Are in Your System?

Try this exercise to know which shells are in your system:

  ls

ls_bin_shell

One of the major components of the UNIX operating system is the UNIX shell, the interface between the user and the operating system itself. The UNIX shell is also equipped with a powerful programming language that helps to exploit the full power of UNIX.

How Shell Programs Operate?

When you login to UNIX, it starts up a shell called the login shell. The login shell then prompts you for commands and executes them. When you enter “exit” or press ctrl-D, you indicate that you want to logoff, thus the shell is terminated.

Shell programs rely on the capability of the UNIX shell to accept commands from the keyboard, but from a file as well.

Simple Program

How to Create a Simple Shell Program?

Suppose that every time after you log in to your system, you often want to determine the current date and time, list the users who are currently logged in, and find out how much disk space is available. You would normally type these commands in sequence, as shown below:

  date
  who
  df -m

date_who_df

Although these commands are simple and short, typing this series of commands might become too tiring if done everyday. A short script might simplify this task. The following exercise creates a shell script called status. (Use vi, vim, gedit or any text editor to create the program).

  vi status

start appending the following commands:

  date
  who
  df -m

display the status of the newly created script

  ls -l status

vi_status

Before you can run this program, you must add the execute permission, using chmod. Otherwise, you will be prompted with a “permission denied” message.

  chmod +x status
  ls -l
  ./status

exec_status

Hello, World

The “Hello, World” program

Most programmers take off with a “Hello, world!” program. The UNIX shell follows the same tradition. Thus, in the next exercise, create the “hello.sh” script with the following line:

  echo "Hello, world\n"

change the permission and run the program.

hello_sh

Comments

Commenting Your Shell Programs

Just like all the good programmers around the world, you should include miscellaneous information or explanations to your shell program. Such information may include programmer’s name, date, reason for creating the program, features, and how the program works, etc. Though these lines are not executed by the system, they give information to other people who may evaluate your program. Likewise, these comments will provide you with valuable reminders several months after you created the program.

Whenever the system encounters a line containing a crosshatch (#), everything from the crosshatch is considered a comment and therefore ignored.

  #title         : hello.sh
  #description   : My Hello Program
  #author        : ariel e. isidro
  #date          : June 21, 2008
  #
  echo "Hello, world!\n"

comment

Variables

Using Variables in Shell Programs

A variable is a memory location used by the shell to store temporary data. Each variable is given a name which is used to access the data. A variable can acquire a value in several ways; the most basic is by assignment which has the following form:

  variable_name=value

For example , consider the following assignment:

  subject=“Operating System”

The statement creates a variable named subject and stores into it the value “Operating System”.

There are several important rules in variable assignments:

  • There can be no space on either side of the equal sign
  • If the value contains any spaces or special characters, it must surrounded by quotation marks. Thus the example above generates a cryptic error if the double quote is removed:
  subject=Operating System

Once a variable has a value, you can retrieve that value and use it as part of a command by supplying the name of the variable, preceded by a dollar sign ($). Whenever the shell sees a dollar sign – variable-name combination, it replaces the combination with the value of the variable during execution of the command.

As an exercise, enter the following commands in you terminal:

  subject="Operating System"
  echo $subject
  echo "Our subject is $subject"

assignment

read

Placing a Value in a Variable with read

Another command for storing value to a variable is read. It allows the system to interact with the user by asking for input from the user and is almost used exclusively inside the shell scripts. The read command is used as follows:

  read [variable_name]

Do the following exercise:

Create a shell script named course.sh

  echo "What is your course? "
  read course
  echo "My course is $course."

read

CLP

Using Command-Line Parameters for Input

Although the read command provides for a method for user input, it is not mostly used in shell programs. Most programs are executed with the required input in the command line. For example, if you want to know the type of file, you type file <filename> rather than enter command file, then ask for the name of the file.

You can also do the same in a shell script. When you type the script name, the shell breaks the command into words and assigns each word to a special variable, called a command-line parameter. These special variables, named $1 to $9, represent the first nine words after the name of the shell script. A word is a group of characters separated by a tab or space. Any phrase that is enclosed in quotation marks is considered as one word.

The following exercises will help you understand this concept:

Create a new shell script named “course2.sh”

  echo "My course is $1"

Change the required permission, and run the script as

  ./course2.sh Engineering

parameters

Exercises

Exercises

  1. Create a shell script that asks for the name of a file; then display the permissions of that file.
  2. Create a shell script that displays the file permission of its command-line parameter.
  3. Some users are comfortable with the read command for input while others are comfortable with command-line parameters. Create a program that:
    1. Prompts the user “What is your last name?” if no command-line parameter is entered. Otherwise, consider the command-line parameter as the last name, if entered. Then,
    2. Display “Hello, Mr. <last_name>!”

Technorati Tags:

You can leave a response, or trackback from your own site.

Leave a Reply

Powered by WordPress | Find BlackBerry Phones for Sale Online. | Thanks to Top Bank CD Rates, Free MMORPG Games and Home Information Packs