0% found this document useful (0 votes)
153 views

Assignment No - 01 Title: Shell Programming: Software Laboratory - Ii

The document discusses shell programming and provides details about shell scripts including components of UNIX operating system like kernel and shell. It describes various shell commands, variables, conditions and loops that can be used in shell scripts. The objective is to study UNIX basics and shell scripting to create a menu driven address book program.

Uploaded by

asegfdfh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views

Assignment No - 01 Title: Shell Programming: Software Laboratory - Ii

The document discusses shell programming and provides details about shell scripts including components of UNIX operating system like kernel and shell. It describes various shell commands, variables, conditions and loops that can be used in shell scripts. The objective is to study UNIX basics and shell scripting to create a menu driven address book program.

Uploaded by

asegfdfh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SOFTWARE LABORATORY – II

Assignment no -01
Title : Shell programming

Problem statement : Write a program to implement an address book with options given
below:
a) Create address book. b) View address book. c) Insert a record. d) Delete a record.
e) Modify a record. f) Exit.
Theory :

Objective
1.To study Unix basic.
2.To study Unix basic shell commands
3. To study shell script and menu driven program.

UNIX Basic:
UNIX is an operating system which was first developed in the 1960s, and has been under
constant development ever since. By operating system, we mean the suite of programs which
make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops
and laptops. UNIX systems also have a graphical user interface (GUI) similar to
Microsoft Windows which provides an easy to use environment. There are many different
versions of UNIX, although they share common similarities. The most popular varieties of
UNIX are Sun Solaris, GNU/Linux, and MacOS X. The UNIX operating system is made up
of three parts; the kernel, the shell and the programs.

The kernel
The kernel is the core of the UNIX operating system. Basically, the kernel is a large
program that is loaded into memory when the machine is turned on, and it controls
the allocation of hardware resources from that point forward. The kernel knows what
hardware resources are available (like the processor(s), the on-board memory, the disk
drives, network interfaces, etc.), and it has the necessary programs to talk to all the devices
connected to it. As an illustration of the way that the shell and the kernel work together,
suppose a user types rm myfile (which has the effect of removing the file myfile). The shell
searches the file store for the file containing the program rm, and then requests the kernel,
through system calls, to execute the program rm on myfile. When the process rm myfile has
finished running, the shell then returns the UNIX prompt to the user, indicating that it is
waiting for further commands.

The shell
The shell acts as an interface between the user and the kernel. The shell is a command line
interpreter (CLI). It interprets the commands the user types in and arranges for them to be
carried out. The commands are themselves programs: when they terminate, the shell gives
the user another prompt .The adept user can customizehis /herown shell and users
can use different shells on the same machine. UNIX system offers verity of shells like 1)
Bourne shell 2) c shell 3) Korn shell 4) Bash shell (very powerful & recommended for use,
Linux default shell) History - The shell keeps a list of the commands you have typed in.
If you need to repeat a command, use the cursor keys to scroll up and down the list or type
history for a list of previous commands.

BVCOEW 1
SOFTWARE LABORATORY – II

Shell Script: Normally shells are interactive. It means shell accept command from you (via
keyboard) and execute them. But if you use command one by one (sequence of 'n' number of
commands),the you can store this sequence of command to text file and tell the shell to
execute this text file instead of entering the commands. This is known as shell script.
Shell Script is series of command written in plain text file.

Shells in Linux

Frequently Used Shell Commands


Command Example Description
ls ls Lists files in current directory
ls -alF List in long format
cd cd tempdir Change directory to tempdir
cd .. Move back one directory
cd ~dhyatt/web-docs Move into dhyatt's web-docs directory
mkdir mkdir graphics Make a directory called graphics
rmdir rmdir emptydir Remove directory (must be empty)
cp cp file1 web-docs Copy file into directory
cp file1 file1.bak Make backup of file1
rm rm file1.bak Remove or delete file
rm *.tmp Remove all file
mv mv old.html new.html Move or rename files
more more index.html Look at file, one page at a time
lpr lpr index.html Send file to printer
man man ls Online manual (help) about command
grep <str><files> grep "bad word" * Find which files contain a certain word
chmod <opt> <file> chmod 644 *.html Change file permissions read only
chmod 755 file.exe Change file permissions to executable
passwd passwd Change passwd
ps <opt> ps aux List all running processes by #ID
ps aux | grep dhyatt List process #ID's running by dhyatt
kill <opt> <ID> kill -9 8453 Kill process with ID #8453
who who Lists who is logged on your machine
history history Lists commands you've done recently
fortune fortune Print random humerous message
date date Print out current date

Exit Status
By default in Linux if particular command/shell script is executed, it return two type of
values which is used to see whether command or shell script executed is successful or not.
(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing
command/shell script.
This value is known as exit status. But how to find out exit status of command or shell script?
In order to determine this exit Status you can use $? special variable of shell.
For e.g. (This example assumes that unknown1file doesn’t not exist on your hard drive)
$ rm unknow1file

BVCOEW 2
SOFTWARE LABORATORY – II

It will show error as follows


rm: cannot remove `unkowm1file': No such file or directory
and after that if you give command
$ echo $?
It will print nonzero value to indicate error.

User defined variables (UDV)


To define UDV use following syntax
Syntax:
variable name=value
'value' is assigned to given 'variable name' and Value must be on right side = sign.

Example:
To define variable called n having value 10
$ n=10
To print or access UDV use following syntax
Syntax:
$variablename

About Quotes

There are three types of quotes:

Quotes
Name Meaning
Double
Quotes "Double Quotes" - Anything enclose in double quotes removed
"
meaning of that characters (except \ and $).
Single
' quotes 'Single quotes' - Enclosed in single quotes remains unchanged.

Back `Back quote` - To execute command


` quote

if condition

if condition which is used for decision making in shell script, If given condition is true then
command1 is executed.
Syntax:
if condition
then
command1 if condition is true or if exit status of condition is 0 (zero)


fi

Condition is defined as:


“Condition is nothing but comparison between two values.”

BVCOEW 3
SOFTWARE LABORATORY – II

For compression you can use test or [ expr ] statements or even exist status can be also used.

Loops in Shell Scripts


while:

The syntax of the while is:


While test-commands
do
commands
done
Execute commands as long as test-commands have an exit status of zero.

for:
The syntax of the for is:
for variable in list
do
commands
done

Each white space-separated word in list is assigned to variable in turn and commands
executed until list is exhausted.

The case Statement


The case statement is good alternative to multilevel if-then-else-fi statement. It enables you to
match several values against one variable. It’s easier to read and write.
Syntax:
case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
pattern N) command
...
..
command;;
*) command
...
..
command;;

The $variable-name is compared against the patterns until a match is found. The shell then
executes all the statements up to the two semicolons that are next to each other. The default is
*) and it’s executed if no match is found.

BVCOEW 4
SOFTWARE LABORATORY – II

Conclusion - The shell acts as an interface between the user and the kernel. Thus shell
use to execute commands. We can store this sequence of command to text file and tell the
shell to execute this text file instead of entering the commands. Shell Script is series of
command written in plain text file.

References –
1. Unix Concepts and Applications By Sumitabha Das, Tata McGraw Hill
2. UNIX Shell Programming by Yashwant Kanetkar, BPB Publications.

BVCOEW 5

You might also like