Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
96 views8 pages

Experiment No. - 8 - Understanding The Basics of Shell Programming 1. Objective(s)

The document discusses shell programming and creating shell scripts. It covers objectives like assessing shell programming skills and constructing scripts using commands. Key points include different shells like Bourne, C, and Korn shells. Procedures are provided for creating simple scripts, using variables, command line parameters, and comments. The goal is to automate tasks and apply programming skills in Unix system administration.

Uploaded by

Carlo Baja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views8 pages

Experiment No. - 8 - Understanding The Basics of Shell Programming 1. Objective(s)

The document discusses shell programming and creating shell scripts. It covers objectives like assessing shell programming skills and constructing scripts using commands. Key points include different shells like Bourne, C, and Korn shells. Procedures are provided for creating simple scripts, using variables, command line parameters, and comments. The goal is to automate tasks and apply programming skills in Unix system administration.

Uploaded by

Carlo Baja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No.

___8__
UNDERSTANDING THE BASICS OF SHELL PROGRAMMING

1. Objective(s):
The activity aims
1.1 To assess programming skills using shell programming
1.2 To construct shell scripts using various commands learned in previous experiments.
1.3 To apply programming skills in creating shell scripts to automate administration of an operating
system.

2. Intended Learning Outcomes (ILOs):


The students shall be able to:
2.1. Integrate various unix commands in a single shell script
2.2. Create shell scripts to automate simple tasks

3. Discussion (not more than 300 words):

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.

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:

3.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.
3.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.
The C-shell prompt is a percent sign (%), rather than the dollar sign ($) used by the Bourne shell.
3.3. 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.
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.
3.4. 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).

4. Resources:
Personal computer with Unix or unix-based operating system

5. Procedure:
5.1. Start a terminal session with your unix or unix-based operating system.

5.2. Create and test shell scripts to automate simple tasks.

5.2.1 Which shells are in your system.

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

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.
5.2.2 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.

5.2.3 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:

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, nano or any text editor to
create the program)

Before you can run this program, you must add the execute permission, using chmod.
Otherwise, you will be prompted with a “permission denied” message
5.2.4 The “Hello, World” program. Most programmers take off with a “Hello, world!” program.
The UNIX shell follows the same tradition. Thus, the next exercise creates the program as
follows:

5.2.5 Commenting 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.
5.2.6 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:
5.2.7 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:

5.2.8 Using the 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:


Course: Experiment No.:
Group No.: Section:
Group Members: Date Performed:
Date Submitted:
Instructor:

6. Data and Results:

7. Conclusion:

8. Assessment (Rubric for Laboratory Performance):

You might also like