Total Pageviews

Friday, 4 April 2014

Scripting Basics !!!

Basics which are helpful in Shell Scripting :

First and most common thing which is more helpful in scripting is ,

1) echo $?      -----  It validates any particular command or a step in a script. If return code is 0 then
statement or command is valid or else it throws a big value for invalid command.

Mostly used in "if" loops.

Let me explain with following example :

root@mysrv1:~# 
root@mysrv1:~#  i+-                        ----- It is a invalid command so echo $? throws error
bash : i+-: command not found
root@mysrv1:~# 
root@mysrv1:~#  echo $?
127
root@mysrv1:~#  i=0                       ----- Since this is valid echo $? throws 0 return code.
root@mysrv1:~# 
root@mysrv1:~# echo $?
0
root@mysrv1:~# 
root@mysrv1:~# 

In "if" loops we use this to say if condition is true so perform the task , if not got to else statement. So the command or the statement in the script which is before " if " condition is valid then if [$?==0] is true so script goes with statement 1 otherwise statement 2.

if [$?==0]
statement 1
else
statement 2

2) Always true while loop,

In some cases we need a task to be done and so we require a true condition so that the task will be given in loop to run in some time intervals..

Example for a always true condition in while loop :     while 2>1

root@mysrv1:~# 
root@mysrv1:~# while 2>1
>do
>metastat -c
>sleep 5
>done
d20              m   30GB d21 d22 (resync-21%)
    d21          s   30GB c1t0d0s1
    d22          s   30GB c1t1d0s1
d10              m   14GB d12 d11 (resync-35%)
    d11          s   14GB c1t0d0s0
    d12          s   14GB c1t1d0s0
d20              m   30GB d21 d22 (resync-21%)
    d21          s   30GB c1t0d0s1
    d22          s   30GB c1t1d0s1
d10              m   14GB d12 d11 (resync-36%)
    d11          s   14GB c1t0d0s0
    d12          s   14GB c1t1d0s0
d20              m   30GB d21 d22 (resync-21%)
    d21          s   30GB c1t0d0s1
    d22          s   30GB c1t1d0s1
d10              m   14GB d12 d11 (resync-36%)
    d11          s   14GB c1t0d0s0
    d12          s   14GB c1t1d0s0
d20              m   30GB d21 d22 (resync-22%)
    d21          s   30GB c1t0d0s1
    d22          s   30GB c1t1d0s1
d10              m   14GB d12 d11 (resync-37%)
    d11          s   14GB c1t0d0s0
    d12          s   14GB c1t1d0s0
^C
root@mysrv1:~# 
root@mysrv1:~# 

Since this condition is always true , this loop executes repeatedly for every 5 secs interval until we interrupt it.

3) Powerful and most helpful in scripts : for loop.

For iterations we need "for" loop. To do same particular thing, like to execute same command for "n" different files or in "n" different servers we use for loops.

For loops in scripting is mainly to reduce burden of performing same task "n" number of times.

We need uname of 20 different servers, we need to list the ip's in a file. Lets say "ips" is file.

root@mysrv1:~# 
root@mysrv1:~# for i in `cat ips`
>do
>ssh $i ;uname -a;
>exit
>done

################################################################################

No comments:

Post a Comment