Experiment No. 6: What Is Shell Scripting?
Experiment No. 6: What Is Shell Scripting?
Experiment No. 6
Being a Linux user means you play around with the command-line. Like it or not, there are
just some things that are done much more easily via this interface than by pointing and
clicking. The more you use and learn the command-line, the more you see its potential. Well,
the command-line itself is a program: the shell. Most Linux distros today use Bash, and this is
what youre really entering commands into.
Now, some of you who used Windows before using Linux may remember batch files. These
were little text files that you could fill with commands to execute and Windows would run
them in turn. It was a clever and neat way to get some things done, like run games in your
high school computer lab when you couldnt open system folders or create shortcuts. Batch
files in Windows, while useful, are a cheap imitation of shell scripts.
Shell scripts allow us to program commands in chains and have the system execute them as a
scripted event, just like batch files. They also allow for far more useful functions, such as
command substitution. You can invoke a command, like date, and use its output as part of a
file-naming scheme. You can automate backups and each copied file can have the current date
appended to the end of its name. Scripts arent just invocations of commands, either. Theyre
programs in their own right. Scripting allows you to use programming functions such as
for loops, if/then/else statements, and so forth directly within your operating systems
interface. And, you dont have to learn another language because youre using what you
already know: the command-line.
Thats really the power of scripting, I think. You get to program with commands you already
know, while learning staples of most major programming languages. Need to do something
repetitive and tedious? Script it! Need a shortcut for a really convoluted command? Script it!
Want to build a really easy to use command-line interface for something? Script it
SHELL: The shell is the command prompt within Linux where you can type commands. If
you have logged into a machine over a network (using ssh or telnet) then the commands you
entered were run by the shell. If you are logged in using a graphical interface then you will
may need to open a terminal client to see the shell. There are several different terminal clients
available such as xterm, konsole and lxterm, or it may be just named Terminal Emulator. It's
location in the menu differs between different distributions if the start menu allows searching
(most do, but not all) then type term and it should show an appropriate terminal, if not then
look under the system or utilities menu.
15BCS1423
Often people seeing the shell will think that this is the UNIX / Linux operating system. It is in
fact a program that is running on top of the operating system. To take a basic view of how
Linux is built up see the diagram below:
1. BOURNE SHELL: The Bourne shell (sh) is a shell, or command-line interpreter, for
computer operating systems.
The Bourne shell was the default shell for Unix Version 7. Most Unix-like systems continue
to have /bin/shwhich will be the Bourne shell, or a symbolic link or hard link to a
compatible shell even when other shells are used by most users.
Developed by Stephen Bourne at Bell Labs, it was a replacement for the Thompson shell,
whose executable file had the same namesh. It was released in 1977 in the Version 7
Unix release distributed to colleges and universities. Although it is used as an interactive
15BCS1423
command interpreter, it was also intended as a scripting language and contains most of the
features that are commonly considered to produce structured programs.
It gained popularity with the publication of The Unix Programming Environment by Brian
Kernighan and Rob Pikethe first commercially published book that presented the shell as a
programming language in a tutorial form.
2.KORN-SHELL: The Korn shell is the UNIX shell (command execution program, often
called a command interpreter ) that was developed by David Korn of Bell Labs as a
comprehensive combined version of other major UNIX shells. Incorporating all the features
of C shell ( csh ) and Tab C-shell ( tcsh ) with the script language features similar to that of
the Bourne shell , the Korn shell is considered the most efficient shell. Korn, Bourne, and C
are the three most commonly used UNIX shells.
The Korn shell is considered a member of the Bourne shell family and uses as its shell
prompt (character displayed to indicate readiness for user input) the $ symbol. Because it is
the easiest shell to use, inexperienced users usually prefer the Korn shell and, not
surprisingly, it is the one most often used in commercial environments.
Sometimes known by its program name ksh , the Korn is the default shell on many UNIX
systems.
3.C-SHELL: C shell is the UNIX shell (command execution program, often called
a command interpreter ) created by Bill Joy at the University of California at Berkeley as an
alternative to UNIX's original shell, the Bourne shell . These two UNIX shells, along with
the Korn shell , are the three most commonly used shells. The C shell program name is csh ,
and the shell prompt (the character displayed to indicate readiness for user input) is
the % symbol. The C shell was invented for programmers who prefer a syntax similar to that
of the C programming language.
The other popular member of the C shell family is called tcsh (for Tab C shell) and is an
extended version of C shell. Some of tcsh's added features are: enhanced history
substitution (which allows you to reuse commands you have already typed), spelling
correction, and word completion (which allows you to type the first couple of letters in a
word and hit the tab key to have the program complete it).
4.BASH-SHELL: Bash is a Unix shell and command language written by Brian Fox for
the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it
has been distributed widely as it is a default shell on the major Linux distributions and OS X.
Bash is a command processor that typically runs in a text window, where the user types
commands that cause actions. Bash can also read commands from a file, called a script. Like
all Unix shells, it supports filename globing (wildcard matching), piping, here
documents, command substitution, variables and control structures for condition-
testing and iteration. The keywords, syntax and other basic features of the language were all
15BCS1423
copied from sh. Other features, e.g., history, were copied from csh and ksh. Bash is
a POSIX shell, but with a number of extensions.
The shell's name is an acronym for Bourne-again shell, punning on the name of the Bourne
shell that it replaces and on the term "born again" that denotes spiritual rebirth in
contemporary American Christianity.
A security hole in Bash dating from version 1.03 (August 1989), dubbed Shellshock, was
discovered in early September 2014 and quickly led to a range of attacks across the Internet.
5.TCSH: is a Unix shell based on and compatible with the C shell (csh). It is essentially the
C shell with programmable command-line completion,command-line editing, and a few other
features. Unlike the other common shells, functions cannot be defined in a tcsh script and the
user must use aliases instead (as in csh).
Experiment No. 7
Commands used:
echo: echo is one of the most commonly and widely used built-in command for Linux bash
and C shells, that typically used in scripting language and batch files to display a line of
text/string on standard output or a file.
PROCEDURE:
1. To depict the use of echo command we have printed our bio-data .firstly create a file with
extension .sh ,then write your bio-data as shown in picture:
15BCS1423
2. The next step is to open terminal of your linux os and type "bash" followed by your name
of file with extension as shown:
Experiment No. 8
Commands used:
1. echo: echo is one of the most commonly and widely used built-in command for Linux
bash and C shells, that typically used in scripting language and batch files to display a line of
text/string on standard output or a file.
2. read: A common use for user-created variables is storing information that a user enters in
response to a prompt. scripts can accept input from the user and store that input in variables.
PROCEDURE:
1. Save your text file with. sh extension and allow the read command to read values of
variables as shown:
15BCS1423
2. go to terminal and type: 'bash' followed by your filename with extension as shown:
15BCS1423
Experiment No. 9
Commands used:
1. echo: echo is one of the most commonly and widely used built-in command for Linux
bash and C shells, that typically used in scripting language and batch files to display a line of
text/string on standard output or a file.
2. read: A common use for user-created variables is storing information that a user enters in
response to a prompt. scripts can accept input from the user and store that input in variables.
PROCEDURE:
15BCS1423
1. Save your text file with .sh extension then using read and echo commands implement all
the arithmetic operators as shown :
2. go to terminal and type: 'bash' followed by your filename with extension as shown: