Saturday 7 July 2012

Shell script - 4

Shell script - 4

To go to Session 1 click here
To go to Session 2 click here
To go to Session 3 click here
To go to UNIX basic commands click here
Online sample UNIX TERMINAL click here

Functions :

           
A function is very use full to break your functionality or program in to little/small programs which will easy to handle. And easy to debug the issue when occurs.

In shell script function can be written with the help of following syntax:

Function_name()
{
            statements
}

Example 13:
#!/bin/sh
funtionone()
{
        echo "I am in funtion one"
        funtiontwo hi how r u
        ret=$?
        echo "funtion two returned $ret"
}
funtiontwo()
{
        echo "I am in funtion two,funtion one called me with $* as arguments i"
        return 1
}
funtionone

Out put:

Testhome/l685360# ./test
I am in funtion one
I am in funtion two,funtion one called me with hi how r u as arguments i
funtion two returned 1
Testhome/l685360#

In the above example 13 there are two functions functionone and functionstwo we call the functionone by the statement funtionone this function in turn calls the second function with arguments “hi how r u” the function two in turn retun back with value 1. I didn’t explain the echo because its all self explanatory.

The sed

            The stream Editor, It is powerful utility of unix, generally used to edit / modify a text files.




The S(Substitution)

Example 14:

Testhome/l685360# vi sed1
Hi baskar,
 i well come u to the festival ,

Testhome/l685360# sed s/baskar/mohan/ <sed1 >sed2
Testhome/l685360# cat sed1
Hi baskar,
 i well come u to the festival ,
Testhome/l685360# cat sed2
Hi mohan,
 i well come u to the festival ,
Testhome/l685360#

In the above example the substitution is used the name baskar in sed1 file  is changed to mohan and saved in another file sed2 the symbol <> refers to redirection look for unix redirection for explanation.


Things to understand
1.      s command
2.      delimiters / / (you can use anything as delimiter as you wish for example (Example 15:
echo jaya | sed s:jaya:jai: ) here we have used : as delimiter)
3.      (baskar) the word to be found
4.      (mohan) the word to be changed finally the command would be
5.      sed s/baskar/mohan/ <inputfile >outputfile
this can also be done for a string for example
           
Example 16:

Testhome/l685360# echo jayachandran | sed s/jaya/jai/
jaichandran
Testhome/l685360#

The &

Example 17:

Testhome/l685360#echo “1365 jai 19b” | sed ‘s/[0-9]*/(&)/’
(1365) jai 19b
Testhome/l685360#

In the above example the sequence or a specific pattern is put between some spl. Char. Like () and so on.


The P

Example 18( without -n):
MTS-REG;titrs105:(l685360)/home/l685360# sed p<sed1
Hi baskar,
Hi baskar,
 i well come u to the festival ,
 i well come u to the festival ,

Example 19( with -n)
MTS-REG;titrs105:(l685360)/home/l685360# sed -n p<sed1
Hi baskar,
 i well come u to the festival ,
MTS-REG;titrs105:(l685360)/home/l685360#

The option p will print the input, if –n is not given the p option alone will make duplicate of each line in the input.


Thanks all...

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