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

Shell Script Assignment

This document provides an introduction to shell scripting. It discusses the history of the Bourne shell and other shells like Korn shell, C shell, and tcsh. It then describes the basic components of a simple "Hello World" shell script like the shebang line, comments, and using echo to print output. The document outlines the advantages of shell scripts like ease of use and ability to link existing programs, and disadvantages like potential errors and slow execution. It concludes by listing 4 basic shell script programs: 1) Printing "Hello World", 2) Using a for loop, 3) Using a while loop, and 4) Performing arithmetic operations.

Uploaded by

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

Shell Script Assignment

This document provides an introduction to shell scripting. It discusses the history of the Bourne shell and other shells like Korn shell, C shell, and tcsh. It then describes the basic components of a simple "Hello World" shell script like the shebang line, comments, and using echo to print output. The document outlines the advantages of shell scripts like ease of use and ability to link existing programs, and disadvantages like potential errors and slow execution. It concludes by listing 4 basic shell script programs: 1) Printing "Hello World", 2) Using a for loop, 3) Using a while loop, and 4) Performing arithmetic operations.

Uploaded by

Ayush Neekhra
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

AYUSH NEEKHRA

18100BTIT03068

Assignment - 2

1.Study shell script


Introduction-
A Brief History of sh-

Steve boure wrote the Bourne shell which appeared in the


Seventh Edition Bell Labs Research version of Unix.
Many other shells have been writter this particular tutorial
concentrates on the Bourne and the Bourne Again shells.
Other shells include the Korn Shell (ksh), the C Shell (csh),
and variations such as tcsh.

Audience-

This tutorial assumes some prior experience namely:

 Use of an interactive Unix/Linux shell


 Minimal programming knowledge - use of variables,
functions, is useful background knowledge
 Understanding of some Unix/Linux commands, and
competence in using some of the more common ones.
(ls, cp, echo, etc)
 Programmers of ruby, perl, python, C, Pascal, or any
programming language (even BASIC) who can maybe
read shell scripts, but don't feel they understand
exactly how they work.

1.
AYUSH NEEKHRA
18100BTIT03068

Philosophy-
Shell script programming has a bit of a bad press amongst some Unix
systems administrators. This is normally because of one of two things:

 The speed at which an interpreted program will run as compared to a C


program, or even an interpreted Perl program.
 Since it is easy to write a simple batch-job type shell script, there are a lot
of poor quality shell scripts around.

It is partly due to this that there is a certain machismo associated with


creating good shell scripts. Scripts which can be used as CGI
programs, for example, without losing out too much in speed to Perl
(though both would lose to C, in many cases, were speed the only
criterion).
There are a number of factors which can go into good, clean, quick,
shell scripts.

Something about shell scripts seems to make them particularly likely


to be badly indented, and since the main control structures are
if/then/else and loops, indentation is critical for understanding what a
script does.

One weakness in many shell scripts is lines such as:

cat /tmp/myfile | grep "mystring"

which would run much faster as:

grep "mystring" /tmp/myfile

Not much, you may consider; the OS has to load up the /bin/grep
executable, which is a reasonably small 75600 bytes on my system,
open a pipe in memory for the transfer, load and run the /bin/cat
executable, which is an even smaller 9528 bytes on my system, attach
it to the input of the pipe, and let it run.

2.
AYUSH NEEKHRA
18100BTIT03068

A First Script-
For our first shell script, we'll just write a script which says "Hello
World". We will then try to get more out of a Hello World program than
any other tutorial you've ever read :-)
Create a file (first.sh) as follows:

First.sh
#!/bin/sh
# This is a comment!
echo Hello World
# This is a comment, too!

The first line tells Unix that the file is to be executed by /bin/sh. This is
the standard location of the Bourne shell on just about every Unix
system. If you're using GNU/Linux, /bin/sh is normally a symbolic link
to bash (or, more recently, dash).

The second line begins with a special symbol: #. This marks the line as
a comment, and it is ignored completely by the shell.
The only exception is when the very first line of the file starts with #! -
as ours does. This is a special directive which Unix treats specially. It
means that even if you are using csh, ksh, or anything else as your
interactive shell, that what follows should be interpreted by the Bourne
shell.
Similarly, a Perl script may start with the line #!/usr/bin/perl to tell your
interactive shell that the program which follows should be executed by
perl. For Bourne shell programming, we shall stick to #!/bin/sh.

The third line runs a command: echo, with two parameters, or


arguments - the first is "Hello"; the second is "World".
Note that echo will automatically put a single space between its
parameters.
The # symbol still marks a comment; the # and anything following it is
ignored by the shell.

now run chmod 755 first.sh to make the text file executable, and run
./first.sh.
Your screen should then look like this:

3.
AYUSH NEEKHRA
18100BTIT03068

$ chmod 755 first.sh


$ ./first.sh
Hello World
$

You will probably have expected that! You could even just run:

$ echo Hello World


Hello World
$

What is Shell Script?


A shell script is a list of commands in a computer program that is run
by the Unix shell which is a command line interpreter. A shell script
usually has comments that describe the steps. The different operations
performed by shell scripts are program execution, file manipulation
and text printing. A wrapper is also a kind of shell script that creates
the program environment, runs the program etc.

Shell Script Types-


In Unix, there are two major types of shells −

Bourne shell − If you are using a Bourne-type shell the $ character is


the default prompt.

C shell − If you are using a C-type shell the % character is the default
prompt.

The Bourne Shell has the following subcategories −

 Bourne shell (sh)


 Korn shell (ksh)
 Bourne Again shell (bash)
 POSIX shell (sh)

The different C-type shells follow −

 C shell (csh)
 TENEX/TOPS C shell (tcsh)

4.
AYUSH NEEKHRA
18100BTIT03068

Advantages of Shell Script


Some of the advantages of shell script are:

 The commands and syntax of the shell script are the same as that entered
at the command line. Because of this, there is no need to switch to a
completely different syntax.
 It is much faster to write a code in shell script than in other programming
languages. This also means that the program is easier to create and files
required can be selected easily.
 Shell script can also be used to provide linkage for already existing
programs.
 Shell scripting can be used by users that are not experts to modify and
tailor the behaviour of their programs according to their requirements.

Disadvantages of Shell Script


Some of the disadvantages of shell script are:

 There may be errors in shell scripting that prove to be quite costly.


 The programs in shell script are quite slow while executing and a new
process is required for every shell command executed.
 Different platforms in shell scripting may also have compatibility
problems.

2.Provide the 4 basic program of


shell scripting.
1. Program of printing "Hello world".

5.
AYUSH NEEKHRA
18100BTIT03068

2. Program of using for loop.

3. Program of using while loop.

6.
AYUSH NEEKHRA
18100BTIT03068

4. Program of using arithmetic operation.

7.

You might also like