Shell Programming:
Usually shells are interactive that mean, they accept command as input from users and execute them. However some time we want to execute a bunch of commands routinely, so we have type in all commands each time in terminal.
A shell program, sometimes referred to as a shell
script, is simply a program constructed of shell commands.
Shell programs are interpreted each time they are run. This
means each command is processed (i.e. executed) by the shell a single line at a
time.
This is different from languages such as C or C++, which are
translated in their entirety by a compiler program into a binary image.
A shell program may be simple and consist of just a few
shell commands, or it may be very complex and consist of thousands of
shell commands.
As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh file extension eg. myscript.sh
Shell Scripts:
The basic concept of a shell script is a list of commands,
which are listed in the order of execution.
A good shell script will have comments, preceded
by # sign, describing the steps.
There are conditional tests, such as value A is greater than
value B, loops allowing us to go through massive amounts of data, files to read
and store data, and variables to read and store data, and the script may
include functions.
It would be a simple text file in which we would put all our
commands and several other required constructs that tell the shell environment
what to do and when to do it.
Shell scripts and functions are both interpreted. This means
they are not compiled.
Example Script
Assume we create a test.sh script.
Note all the scripts would have the .sh extension.
Before you add anything else to your script, you need to alert the system that
a shell script is being started.
- A shell script contains a list of commands which have to be executed regularly.
- Shell script is also known as shell program.
- The user can execute the shell script itself to execute commands in it.
- A shell
script runs in interpretive mode. i.e. the entire script is compiled internally
in memory and then executed.
- Hence, shell scripts run slower than the high-level language programs.
- ".sh" is used as an extension for shell scripts.
Example: A shell script (program1.sh) to execute few commands.
#! /bin/sh
echo “Welcome
to Shell Programming”
echo “Today’s date : `date`” # print date
The hash symbol # indicates the comments in the script.
0 Comments:
Post a Comment