Shell script - 1:
This tutorial will give an over view and a good idea how to write shell script, I think speaking of history like other tutorials is mere waste so I directly starting with details.
If you need any reference or if you are not aware of UNIX basic commands please go through it by clicking here it will be so useful.
All shell scripts start with this #!
Line this is called she bang it tell the what shell script we are going to use
and its location(interpreter location)
Example 1:
#!/bash/sh
For further reference refer
http://en.wikipedia.org/wiki/Shebang_(Unix)
# Comment
The # is used for commenting the a
line
Example 2:
#Adding two numbers
Variables:
Variable name can contain only, (a
to z, A to Z, 0 to 9 and _)
Defining and Assigning
values:
An variable can be defined and assigned
as follows
Var1=10
Var2=”jai”
Variable Types:
When a shell is running, three main types of variables are present:
Local Variables: A local variable is a variable that is present within
the current instance of the shell. It is not available to programs that are
started by the shell. They are set at command prompt.
Environment Variables: An environment variable is a variable that is
available to any child process of the shell. Some programs need environment
variables in order to function correctly. Usually a shell script defines only
those environment variables that are needed by the programs that it runs.
Shell Variables: A shell variable is a special variable that is set by
the shell and is required by the shell in order to function correctly. Some of
these variables are environment variables whereas others are local variables.
Command line arguments
and some special symbols with $
Example program 3:
#!/bin/sh
echo "file name:
$0"
echo "first
argument: $1"
echo "Second
argument: $2"
echo "Number of
arguments: $#"
echo "All the
arguments individually double quoted: $@"
echo "Process
number of previous command:$!"
echo "Process
number of CURRENT SHELL: $$"
echo "All the
arguments double quoted: $*"
Out put:
Testhome/l685360#
./test jaya chandran
file name:
./test
first
argument: jaya
Second
argument: chandran
Number of
arguments: 2
Given
argument is: jaya chandran
Process
number of previous command:
Process
number of CURRENT SHELL: 20775420
All the
arguments: jaya chandran
Testhome/l685360#
Arrays:
Example 4:
#!/bin/sh
name[0]="jayachandran"
name[1]="Mohan"
name[2]="Devi"
name[3]="Shiva"
name[4]="Ranjith"
echo ${name[@]}
echo ${name[*]}
Output
Testhome/l685360# ./test
Mohan Devi Shiva Ranjith
Mohan Devi Shiva Ranjith
Testhome/l685360#
Arithmetic and Relation operators:
The bourne shell does not do any arithmetic it uses external programs expr
and awk to the arithmetic
expr
Example 5:
#!/bin/sh
val1=2
val1=`expr $val1 + 10`
echo "value of val1 aft adding 10 is :
$val1"
Out put:
Testhome/l685360# ./test
value of val1 aft adding 10 is : 12
Testhome/l685360#
In the above program we can see usage of expr
1.
Any
arithmetic expression with symbols should be separated by space.( $val1 + 10 )
2.
Any expr
should be given in between `` only.
To go to next session click here don't forget to put your feed back as comments which encourages me to do this and you can also post your questions or solve/give answers for others questions, Thanks in advance friends.....
To go to next session click here don't forget to put your feed back as comments which encourages me to do this and you can also post your questions or solve/give answers for others questions, Thanks in advance friends.....
No comments:
Post a Comment