Linux Shell Scripting

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 19

Linux Shell Scripting

Scripting,linux programming,BASH scripting

This knol explains how you can starts extending the


functionality of Linux operating system by automating
tasks by using shell programming. I believe that this
will be useful to both beginners and expert as well, I
also like to invite those who interested to add additional
information, thus make this knol out standing , and this
will indeed a big hand to the Linux community....

Contents

• Introduction to Shell Programming


• Variables in Linux
• How to write shell script
• Commands Related with Shell Programming
• Command Line Processing
• Why Command Line arguments required

more
Email Print Favorite

Introduction to Shell Programming


Programming with the Bourne shell (any shell Shell) is similar to
programming in a conventional language. If you've ever written code
in C or Pascal, or even BASIC or FORTRAN, you'll recognize many
common features. For instance, the shell has variables, conditional and
looping constructs, functions, and more. Shell programming is also
different from conventional programming languages. For example, the
shell itself doesn't provide much useful functionality; instead, most
work must be done by invoking external programs. As a result, the
shell has powerful features for using programs together in sequence to
get work done.

Shell program is series of Linux commands. Shell script is just like


batch file is MS-DOS but have more power than the MS-DOS batch file.
Shell script can take input from user, file and output them on screen.
Useful to create our own commands that can save our lots of time and
to automate some task of day today life.

Variables in Linux
Sometimes to process our data/information, it must be kept in
computers RAM memory. RAM memory is divided into small locations,
and each location had unique number called memory location/address,
which is used to hold our data. Programmer can give a unique name to
this

memory location/address called memory variable or variable (Its a


named storage location that may take different values, but only one at
a time). In Linux, there are two types of variable

1) System variables - Created and maintained by Linux


itself. This type of variable defined in CAPITAL LETTERS.

2) User defined variables (UDV) - Created and


maintained by user. This type of variable defined in
lower LETTERS.

Some System variables

You can see system variables by giving command like $ set, Some of
the important System variables are

System Variable Meaning

BASH=/bin/bash Our shell name

BASH_VERSION=1.14.7(1) Our shell version name

COLUMNS=80 No. of columns for our screen

HOME=/home/vivek Our home directory

LINES=25 No. of columns for our screen


LOGNAME=students Our logging name

OSTYPE=Linux Our o/s type : -)

PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings

PS1=[\u@\h \W]\$ Our prompt settings

PWD=/home/students/Common Our current working directory

SHELL=/bin/bash Our shell name

USERNAME=vivek User name who is currently login to this PC

You can print any of the above variables contain as follows

$ echo $USERNAME

$ echo $HOME

Do not modify System variable this can some time create problems.

How to define User defined variables (UDV)

To define UDV use following syntax

Syntax: variablename=value

NOTE: Here 'value' is assigned to given 'variablename' and Value must


be on right side = sign Fore.g.

$ nu=10 # this is ok

$ 10=no # Error, NOT Ok, Value must be on right side of = sign.

To define variable called 'vech' having value truck

$ vech=Truck

To define variable called n having value 10


$ nu=10

Rules for Naming variable name (Both UDV and System


Variable)

(1) Variable name must begin with Alphanumeric character or


underscore character (_), followed by

one or more Alphanumeric character. For e.g. Valid shell variable are
as follows

HOME

SYSTEM_VERSION

vech

no

(2) Don't put spaces on either side of the equal sign when assigning
value to variable. For e.g.. In following variable declaration there will
be no error

$ nu=10

But here there will be problem for following

$ nu =10

$ nu= 10

$ nuo = 10

(3) Variables are case-sensitive, just like filename in Linux. For e.g.

$ nu=10

$ Nu=11

$ NU=20

$ nU=2
Above all are different variable name, so to print value 20 we have to
use $ echo $NO and Not any of the following

$ echo $nu # will print 10 but not 20

$ echo $Nu # will print 11 but not 20

$ echo $nU # will print 2 but not 20

(4) You can define NULL variable as follows (NULL variable is variable
which has no value at the time of definition) For e.g.

$ vech=

$ vech=""

Try to print it's value $ echo $vech , Here nothing will be shown
because variable has no value i.e.

NULL variable.

(5) Do not use ?,* etc, to name your variable names.

How to print or access value of UDV (User defined variables)

To print or access UDV use following syntax

Syntax: $variablename

For eg. To print contains of variable 'vech'

$ echo $vech

It will print 'Truck' (if previously defined as vech=Truck) ,To print


contains of variable 'n' $ echo $n

It will print '10' (if previously defined as n=10)

Caution: Do not try $ echo vech It will print vech instead its
value 'Truck' and $ echo n, It will print

n instead its value '10', You must use $ followed by variable


name.
Q.1.How to Define variable x with value 10 and print it on
screen

$ x=10

$ echo $x

Q.2.How to Define variable xn with value Rani and print it on


screen

$ xn=Rani

$ echo $xn

Q.3.How to print sum of two numbers, let's say 6 and 3

$ echo 6 + 3

This will print 6 + 3, not the sum 9, To do sum or math


operations in shell use expr, syntax is as

follows Syntax: expr op1 operator op2

Where, op1 and op2 are any Integer Number (Number without
decimal point) and operator can be

+ Addition

- Subtraction

/ Division

% Modular, to find remainder For e.g. 20 / 3 = 6 , to find


remainder 20 % 3 = 2, (Remember its

integer calculation)
\* Multiplication

$ expr 6 + 3

Now It will print sum as 9 , But

$ expr 6+3

will not work because space is required between number and


operator (See Shell Arithmetic)

Q.4.How to define two variable x=20, y=5 and then to print


division of x and y (i.e. x/y)

$x=20

$ y=5

$ expr x / y

Q.5.Modify above and store division of x and y to variable


called z

$ x=20

$ y=5

$ z=`expr x / y`

$ echo $z

Note : For third statement, read Shell Arithmetic.

How to write shell script


Now we write our first script that will print "Knowledge is
Power" on screen. To write shell script you can use in of the
Linux's text editor such as vi or mcedit or even you can use cat
command. Here we

are using cat command you can use any of the above text
editor. First type following vi command with filename
myfirst.sh ( It is better to add an extention to the script file so
that you can easily identify those files)

$ vi myfirst.sh

# ‘#’ the symbol is used to add comment/documentation

clear # clear the screen

Echo “Welcome to shell scripting”

Esc and Press ZZ to save and Exit

Now you need to change file permission, to do this

Chmod +x myfirst.sh

Now you are ready to run your first program/binary file

./myfirst.sh

How to Run Shell Scripts

Because of security of files, in Linux, the creator of Shell Script


does not get execution permission by default. So if we wish to
run shell script we have to do two things as follows
(1) Use chmod command as follows to give execution
permission to our script

Syntax: chmod +x shell-script-name

OR Syntax: chmod 777 shell-script-name

(2) Run our script as

Syntax: ./your-shell-program-name

For e.g.

$ ./myfirst.sh

Here '.'(dot) is command, and used in conjunction with shell


script. The dot(.) indicates to current shell that the command
following the dot(.) has to be executed in the same shell i.e.
without the loading of another shell in memory. Or you can
also try following syntax to run Shell Script

Syntax: bash &nbsh;&nbsh; your-shell-program-name

OR /bin/sh &nbsh;&nbsh; your-shell-program-name

For e.g.

$ bash myfirst.sh

Note that to run script, you need to have in same directory


where you created your script, if you are in different directory
your script will not run (because of path settings), For eg. Your
home directory is ( use $ pwd to see current working directory)
/home/vivek. Then you created one script called 'myfirst.sh',
after creation of this script you moved to some other directory
lets say
/home/vivek/Letters/Personal, Now if you try to execute your
script it will not run, since script 'first' is in /home/manoj
directory, to Overcome this problem there are two ways First,
specify complete path of your script when ever you want to run
it from other directories like giving following

command

$ /bin/sh /home/manoj/myfirst.sh

Now every time you have to give all this detailed as you work
in other directory, this take time and you have to remember
complete path. There is another way, if you notice that all of
our programs (in form of executable files) are marked as
executable and can be directly executed from prompt from any
directory (To see executables of our normal program give
command $ ls -l /bin or ls –l /usr/bin) by typing command like

$ bc

$ cc myprg.c

$ cal

etc, How this happed? All our executables files are installed in
directory called /bin and /bin directory is set in your PATH
setting, Now when you type name of any command at $
prompt, what shell do is it first look that command in its
internal part (called as internal command, which is part of
Shell itself, and always available to execute, since they do not
need extra executable file), if found as internal command shell
will execute it, If not found It will look for current directory, if
found shell will execute command from current directory, if not
found, then Shell will Look PATH setting, and try to find our
requested commands executable file in all of the directories
mentioned in PATH settings, if found it will execute it,
otherwise it will give message "bash: xxxx :command not
found", Still
there is one question remain can I run my shell script same as
these executables. Yes you can, for this purpose create bin
directory in your home directory and then copy your tested
version of shell script to this bin directory. After this you can
run you script as executable file without using $ ./shell

script-name syntax, Following are steps

$ cd

$ mkdir bin

$ cp first ~/bin

$ first

Each of above command Explanation

Each of above command Explanation

$ cd Go to your home directory

$ mkdir bin

Now created bin directory, to install your own shell script, so that script can be run as
independent program or can be accessed from any directory

$ cp first ~/bin copy your script 'first' to your bin directory $ first Test whether script is
running or not (It will run) In shell script comment is given with #
character. This comments are ignored by your shell. Comments
are used to indicate use of script or person who
creates/maintained script, or for some programming
explanation etc. Remember always set Execute permission for
you script.

Commands Related with Shell


Programming
(1)echo [options] [string, variables...]

Displays text or variables value on screen.

Options
-n Do not output the trailing new line.

-e Enable interpretation of the following backslash escaped


characters in the strings:

\a alert (bell)

\b backspace

\c suppress trailing new line

\n new line

\r carriage return

\t horizontal tab

\\ backslash

For eg. $ echo -e "An apple a day keeps away \a\t\tdoctor\n"

(2)More about Quotes

There are three types of quotes

" i.e. Double Quotes

' i.e. Single quotes

` i.e. Back quote

1."Double Quotes" - Anything enclose in double quotes


removed meaning of that characters (except

\ and $).

2. 'Single quotes' - Enclosed in single quotes remains


unchanged.

3. `Back quote` - To execute command.

For eg.
$ echo "Today is date"

Can't print message with today's date.

$ echo "Today is `date`".

Now it will print today's date as, Today is Tue Jan ....,See the
`date` statement uses back quote,

(See also Shell Arithmetic NOTE).

(3) Shell Arithmetic

Use to perform arithmetic operations For e.g.

$ expr 1 + 3

$ expr 2 - 1

$ expr 10 / 2

$ expr 20 % 3 # remainder read as 20 mod 3 and remainder is


2)

$ expr 10 \* 3 # Multiplication use \* not * since its wild card)

$ echo `expr 6 + 3`

For the last statement not the following points

1) First, before expr keyword we used ` (back quote) sign not


the (single quote i.e. ') sign. Back

quote is generally found on the key under tilde (~) on PC


keyboards OR To the above of TAB key.

2) Second, expr is also end with ` i.e. back quote.


3) Here expr 6 + 3 is evaluated to 9, then echo command prints
9 as sum

4) Here if you use double quote or single quote, it will NOT


work, For eg.

$ echo "expr 6 + 3" # It will print expr 6 + 3

$ echo 'expr 6 + 3'

Command Line Processing


Now try following command (assumes that the file
"grate_stories_of" is not exist on your disk)

$ ls grate_stories_of

It will print message something like - grate_stories_of: No


such file or directory Well as it turns out ls was the name of an
actual command and shell executed this command when given
the command. Now it creates one question What are
commands? What happened when you type $ ls
grate_stories_of? The first word on command line, ls, is name
of the command to be executed. Everything else on command
line is taken as arguments to this command. For eg.

$ tail +10 myf

Here the name of command is tail, and the arguments are +10
and myf.
Now try to determine command and arguments from following
ommands:

$ ls foo

$ cp y y.bak

$ mv y.bak y.okay

$ tail -10 myf

$ mail raj

$ sort -r -n myf

$ date

$ clear

Command No. of argument to this command Actual Argument

ls 1 foo

cp 2 y and y.bak

mv 2 y.bak and y.okay

tail 2 -10 and myf

mail 1 raj

sort 3 -r, -n, and myf

date 0

clear 0

NOTE: $# holds number of arguments specified on command


line. and $* or $@ refer to all arguments in passed to script.
Now to obtain total no. of Argument to particular script, your
$# variable.

Why Command Line arguments required


Let's take rm command, which is used to remove file, But
which file you want to remove and how you will you tail this to
rm command (Even rm command does not ask you name of file
that would like to remove). So what we do is we write as
command as follows

$ rm {file-name}

Here rm is command and file-name is file which you would like


to remove. This way you tail to rm command which file you
would like to remove. So we are doing one way communication
with our command by specifying file-name. Also you can pass
command line arguments to your script to

make it more users friendly. But how we address or access


command line argument in our script.

Lets take ls command

$ ls -a /*

This command has 2 command line argument -a and /* is


another. For shell script,

$ myshell foo bar

$ myshell foo bar


In shell if we wish to refer this command line argument we
refer above as follows

myshell it is $0

foo it is $1

bar it is $2

Here $# will be 2 (Since foo and bar only two Arguments),


Please note At a time such 9 arguments can be used from
$0..$9, You can also refer all of them by using $* (which
expand to `$0,$1,$2...$9`) Now try to write following for
commands, Shell Script Name ($0), No. of Arguments (i.e. $#),
And actual argument (i.e. $1,$2 etc)

Shell Parameter Description

$1,$2…$9 The positional parameters.

$* Complete set of positional


parameters as a string

$# Number of argument
specified in command line

$0 Name executed command

$? Exit status of last command


$@ Same as $* except when
enclosed in double quotes.

(5)Exit Status

By default in Linux if particular command is executed, it return


two type of values, (Values are used

to see whether command is successful or not) if return value is


zero (0), command is successful, if

return value is nonzero (>0), command is not successful or


some sort of error executing

command/shell script. This value is know as Exit Status of that


command. To determine this exit

Status we use $? variable of shell. For eg.

$ rm unknow1file

It will show error as follows

rm: cannot remove `unkowm1file': No such file or directory

and after that if you give command $ echo $?

it will print nonzero value(>0) to indicate error. Now give


command

$ ls

$ echo $?

It will print 0 to indicate command is successful. Try the


following commands and not down there
exit status

$ expr 1 + 3

$ echo $?

$ echo Welcome

$ echo $?

$ wildwest canwork?

$ echo $?

$ date

$ echo $?

$ echon $?

You might also like