stickyraffleΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 4 χρόνια και 8 μήνες)

161 εμφανίσεις

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

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

(in contr
ast with the

that contains time
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

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

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

What is Shell Script?

Shell Script is

series of command


in plain text
. 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

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
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

Why to Write Shell Script?

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

Useful to create our own commands.

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.


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





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

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


Many modern shells also supply various features usually
found only in more sophisticated general
purpose programming
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
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

directories where the program

be stored. This path
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.


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


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".)



plain text editor will work.

Vi (Command Line Unix)

Notepad (Windows)

TextEdit (MAC OS)


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

Different shells available are :

C Shell

Korn Shell

Z Shell


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 ''.


echo "Hello World"

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

chmod 744


chmod +x

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$ ./

Hello World

If you want to see the execution step

which is very
useful for troubleshooting

then execute it with the '
nd arguments') option:


+ 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


echo Hello World

Comments in a Shell

In shell scripting, all lines beginning with # are

# 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

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.


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.

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.

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

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


Defined Variables

These are the variables that are normally used in

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

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.,


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

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

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:




local local_var=100


echo "local variable is $local_var"

echo "global variable i
s $global_var"


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


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



echo "statement to be displayed"


echo command

display the value in

define_tempval, we have to use


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

bash$ echo $define_tempval



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



echo "The username is $username"


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
arguments supplied; this

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

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':


echo "The first variable is $1"

echo "The second variable is $2"

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

bash$ ./ blessen lijoe

The first variable is blessen

The second variable is lijoe

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


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

bash$ cat test

Then, check the value of $?.

bash$ echo $?


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 $?


The value 1

shows that the above command was unsuccessful.

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'.


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
bracket expression starting with a "$" sign. The syntax

$[expression operation statement]


echo $[12 + 10]

Conditional Statements

A. ‘if’ Statement:

The syntax is show below:

if [ conditional statement ]


... Any commands/statements ...


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".


cho "Enter your username:"

read username

if [ "$username" = "blessen" ]


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


echo 'Sorry, wrong username.'


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’.


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))






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


echo $i


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>




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



for i in `cat /etc/group`


count=`expr "$count" + 1`

echo "Line $count is being displayed"

echo $i


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.

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 ]




The following script assigns the value "1" to th


and adds one to the value of


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


is less than



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

The loop variable must be initialized before loop

And the loop must not be infinite.

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>




Syntax of case:

case $<variable> in

<option1>) statements ;;

n2>) statements ;;

*) echo "Sorry, wrong option" ;;


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

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


is a wildcard(similar to default in C).


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



# end of function

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


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 '
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.


s Of Shell Script:

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

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

A shell script can be used to provide a sequencing and
linkage around existing programs, and for
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.

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


Of Shell Script:

hell scripting is prone to costly errors.


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,

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

There are also compatibility problems between different

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