Shell_Scriptx

stickyraffleSoftware and s/w Development

Nov 4, 2013 (3 years and 11 months ago)

139 views

Shell Script



In the early days computers where used to run programs,
and nothing more. You punched your program on cards, delivered
the pack to the computer department. The staff there loaded
the program into the computer, executed it and retrieved the
result on paper. This was in turn returned to you and you had
to sit down and figure out why you got the result you got.

Modern computers are a little more complex than that. You
there have a complete environment where you can execute your
programs and eve
n have such astonishing things as
interactive

programs. It would of course be possible to write specially
designed programs that formatted the output of your programs
according to your wishes, but the number of specialized
programs would quickly increase,
leaving your computer loaded
with "might come in handy" programs.

A better approach would be to have a small set of
processing programs together with a program made to "glue the
parts together." On a UNIX system such a program is called the
shell

(in contr
ast with the
core

that contains time
-
sharing
code, file access code and other system oriented code). The
shell is used to issue commands, start processes, control
jobs, redirect input and output, and other mundane things that
you do on a modern computer.

A shell script is a script written for the shell, or
command line interpreter, of an operating system. The shell is
often considered a simple domain
-
specific programming
language.

Typical operations performed by shell scripts
include file manipulation, pro
gram execution, and printing
text.

In its most basic form, a shell script can provide a
convenient variation of a system command where special
environment settings, command options, or post
-
processing is
applied automatically, but in a way that allows the
new script
to still act as a fully normal Unix command.

Shell scripts allow several commands that would be
entered manually at a command line interface to be executed
automatically, and without having to wait for a user to
trigger each stage of the sequenc
e


What is Shell Script?

"
Shell Script is

series of command

written

in plain text
file
. Shell script is just like batch file is MS
-
DOS but have
more power than the MS
-
DOS batch file."

A shell script is a script written for the shell, or
command line
interpreter, of an operating system. The shell is
often considered a simple domain
-
specific programming
language. Typical operations performed by shell scripts
include file manipulation, program execution, and printing
text.

It's called a shell script beca
use it combines into a
"script" in a single file a sequence of commands that would
otherwise have to be presented to the system from a keyboard
one at a time. The shell is the operating system's command
interpreter and the set of commands you use to commun
icate
with the system. A shell script is usually created for command
sequences for which a user has a repeated need. You initiate
the sequence of commands in the shell script by simply
entering the name of the shell script on a command line.

In the DOS op
erating system, a shell script is called a batch
file. In IBM's mainframe VM operating systems, it's called an
EXEC.


Why to Write Shell Script?



Shell script can take input from user, file and output them
on screen.



Useful to create our own commands.



Save
lots of time.



To automate some task of day today life.



System Administration part can be also automated



Setting Variables


Shell Variables:

Values of shell variable are all character
-
based: A value
is formally defined to be a list of zero or more
elements, and
an element is formally defined to be a character string. In
other words, a shell variable consists of an array of strings.

For example,

set X

will set the variable $X to have an empty list as its value.

The command

set V = abc

will set V to
have the string ‘abc’ as its value.

The command

set V = (123 def ghi)

will set V to a list of three elements, which are the strings
‘123’, ‘def’ and ‘ghi’.

The several elements of a list can be treated like array
elements. Thus for V in the last example a
bove, $V[2] is the
string ‘def’. We could change it, say to ‘abc’, by the command

set V[2] = abc


There is a set of other predefined variables used for
different purposes related to the behaviour

of the shell. We
will here go through the more important ones and describe
their purpose.

1. HOME

This is the home directory of your account. It is also
used as the default when typing cd without any arguments.


2. PS1

The primary prompt string, norma
lly set to `$

'. This is
what the computer prints whenever it is ready to process
another command.


3. PS2

The secondary prompt string, normally set to `>

'. The
shell prints this prompt whenever you have type an incomplete
line, e.g., if you are missing a closing quote. For example:


$
echo 'hello


>
world'


hello


world



4. SHELL

This variables give the path of the shell y
ou are
currently executing, e.g., if you are executing the standard
Bourne shell, the SHELL variables is usually set to /bin/sh.


5. IFS

This is set to the
internal field separators
, normally
set to space, tab, and blank.


Like standard system commands, s
hell scripts classically
omit any kind of filename extension unless intended to be read
into a running shell through a special mechanism for this
purpose






Language Constructs

The shell script language, like other programming
languages, has constructs f
or conditional execution (ifthen
-

else; while), iterative execution (for loop), a switch
statement, and a goto statement
.


Programming

Many modern shells also supply various features usually
found only in more sophisticated general
-
purpose programming
lang
uages, such as control
-
flow constructs, variables,
comments, arrays, subroutines, and so on. With these sorts of
features available, it is possible to write reasonably
sophisticated applications as shell scripts. However, they are
still limited by the fact

that most shell languages have
little or no support for data typing systems, classes,
threading, complex math, and other common full language
features, and are also generally much slower than compiled
code or interpreted languages written with speed as a
performance goal.


The PATH variable

One of the more important variables is the
path variable
.
The path variable controls where the shell searches for
commands, when you type them to at the prompt. Let us go
through the process in detail.

When you type a
non
-
built in

command to the shell, the
shell searches for a program to execute. The programs are
simply executable files somewhere in the file system; they are
executable either because they are compiled programs (written
in C, C++, Pascal, Ada, or some ot
her language) or because
they are scripts that may be executed. Since we don't want to
go through all files in the file system (on my account alone,
I have approximately 3700 files), we have a
path

of
directories where the program
may

be stored. This path
is
given to the shell as a
colon separated

list of directories
stored in the environment variable PATH. To change the
directories where your shell should look, just alter the value
of PATH.

Example:

If your path contains `/usr/bin:/bin:/usr/local/bin'
you
may extend the path with /home/matkin/bin (which could be
the place where you put your own scripts) by writing

$
PATH=/home/matkin/bin:$PATH


The effect of this only remains as long as you are logged in.
If you log out, your changes will be undone since e
very time
you start a new shell: each shell starts with a fresh set of
variables. To set the path each time you log in, you have to
add a line to the startup file
.


Writing A She
ll Script:

Shell scripting can be defined as a group of commands
executed in sequence. Let's start by describing the steps
needed to write and execute a shell script:


Step 1:

Open the file using an editor (e.g., "vi" or "pico".)

vi Firstshellscript.sh

Any

plain text editor will work.



Vi (Command Line Unix)



Notepad (Windows)



TextEdit (MAC OS)



EditPlus


Step 2:

All shell scripts should begin with "#!/bin/bash" or
whatever other shell you prefer. This line is called the
shebang, and although it looks like a c
omment, it's not: it
notifies the shell of the interpreter to be used for the
script. The provided path must be an absolute one (you can't
just use "bash", for example), and the shebang must be located
on the first line of the script without any preceding
space.

Different shells available are :



C Shell



Korn Shell



Z Shell



bash


Step 3:

Write the code that you want to develop. Our first
shell script will be the usual "Hello World" routine, which
we'll place in a file called 'Firstshellscript.sh'.

#!/bin/sh

echo "Hello World"


Step 4:
The next step is to make the script executable by using
the "chmod" command.

chmod 744 Firstshellscript.sh

or

chmod +x Firstshellscript.sh


Step 5:

Execute the script. This can be done by entering the
name of the script on the co
mmand line, preceded by its path.
If it's in the current directory, this is very simple:

bash$ ./Firstshellscript.sh

Hello World

If you want to see the execution step
-
by
-
step
-

which is very
useful for troubleshooting
-

then execute it with the '
-
x'
('expa
nd arguments') option:

sh
-
x Firstshellscript.sh

+ echo 'Hello World'

Hello World


To see the contents of a script, you can use the 'cat' command
or simply open the script in any text editor:

bash$ cat Firstshellscript.sh

#!/bin/sh

echo Hello World








1.
Comments in a Shell

In shell scripting, all lines beginning with # are
comments.

# This is a comment line.

# This is another comment line.

You can also have comments that span multiple lines by
using a colon and single quotes:

: 'This is a comment
line.

Again, this is a comment line.

My God, this is yet another comment line.'

Note: This will not work if there is a single quote mark
within the quoted contents.


2.
Variables

As you may or may not know, variables are the most
significant part of any pr
ogramming language, be it Perl, C,
or shell scripting. In the shell, variables are classified as
either system variables or user
-
defined variables.


3.
System Variables

System variables are defined and kept in the environment
of the

parent shell

(the shell

from which your script is
launched.) They are also called environment variables. These
variable names consist of capital letters, and can be seen by
executing the 'set' command. Examples of system variables are
PWD, HOME, USER, etc. The values of these sy
stem variables can
be displayed individually by "echo"ing the system variables.
E.g.,

echo $HOME

will display the value stored in the system
variable HOME.

When setting a system variable, be sure to use the "export"
command to make it available to the

chil
d shells

(any shells
that are spawned from the current one, including scripts):

bash$ SCRIPT_PATH=/home/blessen/shellscript

bash$ export SCRIPT_PATH

Modern shells also allow doing all this in one pass:

bash$ export SCRIPT_PATH=/home/blessen/shellscript


4.

User
-
Defined Variables

These are the variables that are normally used in
scripting
-

ones that you don't want or need to make available
to other programs. Their names cannot start with numbers, and
are written using lower case letters and underscores by
c
onvention
-

e.g. 'define_tempval'.

When we assign a value to a variable, we write the
variable name followed by '=' which is immediately followed by
the value, e.g.,

define_tempval=blessen

(note that there must
not be any spaces around the equals sign.)


The ‘$’ sign is used to retrieve the contents of a
variable.


5. Scope of a Variable

In shell programming, we also use the scope of a variable
for various programming tasks
-

although this is very rarely
necessary, it can be a useful tool. In the shell,
there are
two types of scope: global and local. Local variables are
defined by using a "local" tag preceding the variable name
when it is defined; all other variables, except for those
associated with function arguments, are global, and thus
accessible fro
m anywhere within the script. The script below
demonstrates the differing scopes of a local variable and a
global one:

#!/bin/sh


display()

{


local local_var=100


global_var=blessen


echo "local variable is $local_var"


echo "global variable i
s $global_var"

}


echo "======================"

display

echo "=======outside ========"

echo "local variable outside function is $local_var"

echo "global variable outside function is $global_var"

Running the above produces the following output:

============
==========

local variable is 100

global variable is blessen

=======outside ========

local variable outside function is

global variable outside function is blessen

Note the absence of any value for the local variable outside
the function.


6. Input and Out
put in Shell Scripting

For accepting input from the keyboard, we use

read. This
command will read values typed from the keyboard, and assign
each to the variable specified for it.

The syntax of read is :

read <variable_name>

For output, we use
the

echo

command.

echo "statement to be displayed"


7
.

echo command
:


To
display the value in

define_tempval, we have to use
the

echo

command and precede the variable name with a '$'
sign, i.e.:

bash$ echo $define_tempval

blessen


Example:

The following s
cript sets a variable named "username" and
displays its content when executed.

#!/bin/sh


username=blessen

echo "The username is $username"


8
.
Command

line Arguments
:

These are variables that contain the arguments to a
script when it is run. These
variables are accessed using $1,
$2, ... $n, where $1 is the first command
-
line argument, $2
the second, etc. Arguments are delimited by spaces. $0 is the
name of the script.

The variable $# will display the number of command
-
line
arguments supplied; this

number is limited to 9 arguments in
the older shells, and is practically unlimited in the modern
ones.

The variable $@ displays all command line arguments.

Consider a script that will take two command
-
line arguments
and display them. We'll call it 'comman
dline.sh':

#!/bin/sh


echo "The first variable is $1"

echo "The second variable is $2"


When I execute 'commandline.sh' with command
-
line arguments
like "blessen" and "lijoe", the output looks like this:

bash$ ./commandline.sh blessen lijoe

The first variable is blessen

The second variable is lijoe



9.
Exit status variable

This variable tells us if the last command executed was
successful or not. It is represented by $?. A value of 0 means
that the command was successful.

Any other number means that
the command was unsuccessful (although a few programs such as
'mail' use a non
-
zero return to indicate status rather than
failure.) Thus, it is very useful in scripting.

To test this, create a file named "test", by running

touc
h
test

. Then, "display" the content of the file:

bash$ cat test

Then, check the value of $?.

bash$ echo $?

0

The value is zero because the command was successful. Now try
running 'cat' on a file that isn't there:

bash$ cat xyz1

bash$ echo $?

1

The value 1

shows that the above command was unsuccessful.


10.
Arithmetic Operations in Shell Scripting

Like other scripting languages, shell scripting also
allows us to use arithmetic operations such as addition,
subtraction, multiplication, and division. To use th
ese, one
uses a function called

expr; e.g., "expr a + b" means 'add a
and b'.

e.g.:

sum=`expr 12 + 20`

Similar syntax can be used for subtraction, division, and
multiplication. There is another way to handle arithmetic
operations; enclose the variables and

the equation inside a
square
-
bracket expression starting with a "$" sign. The syntax
is

$[expression operation statement]

e.g.:

echo $[12 + 10]


11.
Conditional Statements

A. ‘if’ Statement:


The syntax is show below:

if [ conditional statement ]

then


... Any commands/statements ...

fi

The script cited below will prompt for a username, and if the
user name is "blessen", will display a message showing that I
have successfully logged in. Otherwise it will display the
message "wrong username".

#!/bin/sh


e
cho "Enter your username:"

read username


if [ "$username" = "blessen" ]

then


echo 'Success!!! You are now logged in.'

else


echo 'Sorry, wrong username.'

fi

Remember to always enclose the variable being tested in double
quotes; not doing so will cause yo
ur script to fail due to
incorrect syntax when the variable is empty. Also, the square
brackets (which are an alias for the 'test' command) must have
a space following the opening bracket and preceding the
closing one.

The end of ‘if’ statement is indicate
d using ‘fi’.


B. ‘elseif’ and ‘else’ Statements:




‘elseif’ is written as ‘elif’.


12.
Loops

A.
The "for" Loop

The most commonly used loop is the "for" loop. In shell
scripting, there are two types: one that is similar to C's
"for" loop, and an iterator

(list processing) loop.


Syntax for the first type of "for" loop (again, this type
is only available in modern shells):

for ((initialization; condition; increment/decrement))

do


...statements...

d
one


Example:

#!/bin/sh


for (( i=1; $i <= 10; i++ ))

do


echo $i

done

This will produce a list of numbers from 1 to 10.


The syntax for the second, more widely
-
available, type of
"for" loop is:

for <variable> in <list>

do


...statements...

done

This script will read the contents of '/etc/group' and display
each
line, one at a time:

#!/bin/sh


count=0

for i in `cat /etc/group`

do


count=`expr "$count" + 1`


echo "Line $count is being displayed"


echo $i

done


echo "End of file"


‘for’ loop can also be written as

for i in 2 4 6

Loop is executed for i=2,i=4 and i=6.

B.
While Loop

The "while" loop is another useful loop used in all
programming languages; it will continue to execute until the
condition specified becomes false.

while [ condition ]

do


...statement...

done

The following script assigns the value "1" to th
e
variable

num

and adds one to the value of

num

each time it
goes around the loop, as long as the value of

num

is less than
5.

#!/bin/sh


num=1


while [$num
-
lt 5]; do num=$[$num + 1]; echo $num; done


The loop variable must be initialized before loop
starts.

And the loop must not be infinite.


C.
Select and Case Statement

Similar to the "switch/case" construct in C programming,
the combination of "select" and "case" provides shell
programmers with the same features. The "select" statement is
not part
of the "case" statement, but I've put the two of them
together to illustrate how both can be used in programming.

Syntax of select:

select <variable> in <list>

do


...statements...

done


Syntax of case:

case $<variable> in


<option1>) statements ;;


<optio
n2>) statements ;;


*) echo "Sorry, wrong option" ;;

e
sac


The end of case statement is done using “esac”.(reverse of
case).

The code for each option is ended using “;;” .

*

is a wildcard(similar to default in C).


13.
Functions

In the modern world where a
ll programmers use the OOP
model for programming, even we shell programmers aren't far
behind. We too can break our code into small chunks called
functions, and call them by name in the main program. This
approach helps in debugging, code re
-
usability, etc
.

Syntax for "function" is:

<name of function> ()

{

# start of function


statements

}

# end of function

Functions are invoked by citing their names in the main
program, optionally followed by arguments. For example:

#!/bin/sh


sumcalc ()

{


sum=$[$1 + $2]

}


echo "Enter the first number:"

read num1

echo "Enter the second number:"

read num2


sumcalc $num1 $num2


echo "Output from function sumcalc: $sum"


Debugging Shell Scripts

Now and then, we need to debug our programs. To do so, we
use the '
-
x' and '
-
v'
options of the shell. The '
-
v' option
produces verbose output. The '
-
x' option will expand each
simple command, "for" command, "case" command, "select"
command, or arithmetic "for" command, displaying the expanded
value of PS4, followed by the command and
its expanded
arguments or associated word list. Try them in that order
-

they can be very helpful when you can't figure out the
location of a problem in your script.







Comparators
:








Advantage
s Of Shell Script:



Often, writing a shell script is much quicker than writing
the equivalent code in other programming languages.



E
asy program or file selection, quick start, and
interactive debugging.



A shell script can be used to provide a sequencing and
decision
-
making
linkage around existing programs, and for
moderately
-
sized scripts the absence of a compilation step
is an advantage.



Interpretive running makes it easy to write debugging code
into a script and re
-
run it to detect and fix bugs.



Non
-
expert users can use
scripting to tailor the behavior
of programs, and shell scripting provides some limited
scope for multiprocessing.


Disadvantages

Of Shell Script:



S
hell scripting is prone to costly errors.



A

single extra space converts the command from one that
deletes e
verything in the sub
-
directories to one which
deletes everything
-

and also tries to delete everything in
the root directory.



Similar problems can transform cp and mv into dangerous
weapons, and misuse of the > redirect can delete the
contents of a file.



This is made more problematic by the fact that many UNIX
commands differ in name by only one letter: cp, cd, dd, df,
etc.



Another significant disadvantage is the slow execution
speed and the need to launch a new process for almost every
shell command exec
uted.



There are also compatibility problems between different
platforms.



Similarly, more complex scripts can run into the
limitations of the shell scripting language itself; the
limits make it difficult to write quality code and
extensions by various she
lls to ameliorate problems with
the original shell language can make problems worse
.