Tuesday, October 21, 2014

SHELL SCRIPT PART -1

Shell script structure:
1.which shell need to interpret.
2. Comments related to scrip.
3. Comments on purpose of script.
4. Comments on mode of execution.
5. Define files and variables.
6. Define functions.
7. Define Main.
8. END OF SCRIPT
Example shift operator:
#!/usr/bin/bash
a=$#
for i in 1 .. $a
do
Token=$1
echo "token $Token"
shift
done
#bash shifteg 1 2 3
token 1
token 2
token 3
TRAP:
When a program is terminated before it would normally end, we can catch an exit
signal. This is called a trap.
0      —              Normal termination, end of script
1     SIGHUP     Hang up, line disconnected
2     SIGINT       Terminal interrupt, usually Ctrl + C
3     SIGQUIT    Quit key, child processes to die before terminating
9     SIGKILL     kill -9 command, cannot trap this type of exit status
15    SIGTERM   kill command’s default action
19    SIGSTOP   Stop, usually Ctrl + z
Note:- SIGKILL [9] is not trappable
Example:-
#cat trapeg
trap 'echo "\nEXITING on a TRAPPED SIGNAL"' 1 2 3 15 19
while true
do
echo
sleep 10
done
#bash trapeg


EXITING on a TRAPPED SIGNAL  -->hear tested with kill sig 1[ took another session kill -1 pid]

EXITING on a TRAPPED SIGNAL -->hear tested with killsig 2

EXITING on a TRAPPED SIGNAL -->hear tested with killsig 3

EXITING on a TRAPPED SIGNAL -->hear tested with killsig 15

EXITING on a TRAPPED SIGNAL -->hear tested with killsig 19


Killed  ---->hear tested with kilsig 9

In the above program u need to stop execution on receiving any signal  use following
trap 'echo "\nEXITING on a TRAPPED SIGNAL";exit' 1 2 3 15 19
eg:
cat trapeg
trap 'echo "\nEXITING on a TRAPPED SIGNAL";exit' 1 2 3 15 19
while true
do
echo
sleep 10
done
sairam@solaris:~$ bash trapeg
EXITING on a TRAPPED SIGNAL --->tried with kill -1 <pid>
=====================
Different Types of Shells to Declare
COMMAND DESCRIPTION
#!/bin/sh     or  #!/usr/bin/sh                        Declares a Bourne shell
#!/bin/ksh   or  #!/usr/bin/ksh                      Declares a Korn shell
#!/bin/csh   or  #!/usr/bin/csh                      Declares a C shell
#!/bin/bash or  #!/usr/bin/bash                   Declares a Bourne-Again (Bash) shell
SHIFT command:-
# cat shift.sh
#!/usr/bin/Bash
echo "welcome to shift program PLEASE ENTER ARUMETNS"
echo "ALL parameters :$@"
shift
echo "AFTER EXECTING SHFIT COMMAND ALL Parameters :$@"
# sh shift.sh 1 2 3 4
welcome to shift program PLEASE ENTER ARUMETNS
ALL parameters :1 2 3 4
AFTER EXECTING SHFIT COMMAND ALL Parameters :2 3 4

Special Parameter Definitions
The $*  special parameter specifies all command-line arguments.
The $@  special parameter also specifies all command-line arguments.
The "$*"  special parameter takes the entire list as one argument with spaces between.
The "$@"  special parameter takes the entire list and separates it into separate arguments.
The $$  special parameter specifies Process id.
The  !!    special parameter for execution of previous command  in bash shell.
The $# special parameter specifies  total of arguments

Communicating with Users:-
wall                   Writes a message on the screen of all logged-in users on the local host.
rwall                Writes a message on the screen of all logged-in users on a    remote host.
Write              Writes a message to an individual user. The user must currently be logged in.
 TALK                      Starts an interactive program that allows two users to have  a conversation. The screen is split in          two, and                                both users  can see what each person is typing.

Wall demo:
root@solaris:~# wall
wlecome
Broadcast Message from root (pts/1) on solaris Thu Mar 21 12:41:58...
Welcome
test@solaris:~$ Broadcast Message from root (pts/1) on solaris Thu Mar 21 12:41:58...
welcome
…..
WRITE DEMO:-
root@solaris:~# write test
who r y
what happend
welcome

test@solaris:~$
        Message from root on solaris (pts/1) [ Thu Mar 21 12:43:15 ] ...
who r y
what happend
welcome
<EOT>
TaLK:
#svcs talk
STATE          STIME    FMRI
online         12:32:29 svc:/network/talk:default
Root user:
root@solaris:~# talk test
[Connection established]
welcome to test




└------------------------------------------------------------------------------┘
hi root





TEST user:
Message from Talk_Daemon@solaris at 12:45 ...
talk: connection requested by root@localhost.
talk: respond with:  talk root@localhost
talk root@localhost
[Connection established]
hi root





└------------------------------------------------------------------------------┘

welcome to test

Silent Running:-
15 3 8 1 * /usr/local/bin/somescript.Bash 2>&1 >/dev/null
We can call this silent running. This means that there is absolutely no output from the script going to our screen. Inside the script there may be some output directed to files or devices, a particular terminal, or even the system’s console, /dev/console, but none to the user’s screen. This is especially useful when executing a script from one of the system’s cron tables.
2>&1 > /dev/null
By terminating a command like this it redirects standard error (stderr), specified by file descriptor 2, to standard output (stdout), specified by file descriptor 1. Then we have another redirection to /dev/null, which sends all of the output to the bit bucket.

How to execute commands on Remote shell:-
#ssh user@hostname command_to_execute
# cat sshcopy
#!/bin/Bash
#
# SCRIPT: keyit.rsa
# PURPOSE: This script is used to set up RSA SSH keys.
This script must be executed by the user who needs the keys setup.
REM_HOST=$1
cat $HOME/.ssh/id_rsa.pub | ssh $REM_HOST "cat >> ~/.ssh/authorized_keys"
example:
#cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApqBrhDRapJ/V0C3oblc7kwIRBFfW1pH2xs++aZ5Pwn8xtM/kIRkk66eKnuGBhBdW/SxI9cxOB1jMXFN1vNjmr9LGhefE/AE9V9T3ZT69Lc8hHNsOO+wFJca7J5ozY0yhOkuzLyTbuwUmr2ywym7lHeZQJfsPoQzzLVqCHb4/er+vNKKfTjPz87uqvCQgYJHEVEnMzCbaweHtoXYfRjmk8dv9Ew/6EuJ0VhtLS9WR+iFBMVfeNk8S79YG2JUOa/ijzjFORSFSTPjGRjb5q3QBmamSN2T6loLSQ0T/lyP5oAjah5A6OhF31qgPGKxCBHYSbWf1+7m7/1ciB8iVrZHTXQ== root@solaris
# cat /root/.ssh/id_rsa.pub|ssh 10.25.12.89 "cat >>/.ssh/authorized_keys"
Password:
# ssh 10.25.12.89
Last login: Mon Mar 25 15:41:05 2013 from 10.66.10.101
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
You have new mail.
Sourcing //.profile-EIS.....
root@SAPTEST3 # exit
Connection to 10.25.12.89 closed.



Uppercase or Lowercase Text for Easy Testing:-
# echo "welcome"|tr '[a-z]' '[A-Z]'
WELCOME

# echo "welcome TO Reliance"|tr '[A-Z]' '[a-z]'
welcome to reliance

[or]
For upper case letters:-
#typeset -u variable
# variable="welcome to reliance"
# echo $variable
WELCOME TO RELIANCE
For lower case letters:-
#typeset -l a
# a="WELCOME TO RELIANCE"
# echo $a
welcome to reliance

Using getopts to Parse Command-Line Arguments:-
Syntax:-
getopts OptionString VARIABLE
eg :-
getopts :s:m:h:d:p: TIMED 2>/dev/null

#cat one.sh
#!/usr/bin/Bash
while getopts :s:m:h:d:y:  timed 2>/dev/null ==>if u don’t mention y: last value not accepted
do
case $timed in
s)
        echo "seconds :$OPTARG"
        ;;
m)
        echo "minutes :$OPTARG"
        ;;
h)
        echo "hours : $OPTARG"
        ;;
y)
        echo "years : $OPTARG"
        ;;
*)
        echo "please use -s -m -h -d -y options"
        ;;
esac
done
#sh one.sh -s 10 -m 20 -h 24 -m 10 -y 1996
seconds :10
minutes :20
hours : 24
minutes :10
years : 1996

script to report BCV sync:-
# cat test
#!/bin/sh
MAILTEST=/usr/local/bin/emc/scripts/P22-B2/maillog
COUNT_TIME=`grep -i IST $MAILTEST/EMC_SAR_b2_9May2013.log.tmp|tail |wc -l`
grep -i IST $MAILTEST/EMC_SAR_b2_9May2013.log.tmp|tail>/tmp/time
grep -i "TOTAL Sync" $MAILTEST/EMC_SAR_b2_9May2013.log.tmp|tail |cut -d':' -f2>/tmp/Tsync
grep -i "Change Log" $MAILTEST/EMC_SAR_b2_9May2013.log.tmp|tail |cut -d':' -f2>/tmp/CL
grep -i "Change Data" $MAILTEST/EMC_SAR_b2_9May2013.log.tmp|tail |cut -d':' -f2>/tmp/CD
echo "Content-type: text/html" >>/tmp/sync
echo "<HTML>">>/tmp/sync
echo "<BODY>">>/tmp/sync
echo "<table border="1" bgcolor="#00FF00"> ">>/tmp/sync
echo "<tr bgcolor="#FF6347">">>/tmp/sync
echo "<td>RetailPD B2 start time</td>">>/tmp/sync
echo "<td>RetailPD B2 TOTAL Sync time</td>">>/tmp/sync
echo "<td>Net Change Log (MB)</td>">>/tmp/sync
echo "<td>Net Change Data (MB)</td>">>/tmp/sync
echo "</tr>">>/tmp/sync
for ((i=1;i<=$COUNT_TIME;i++))
do
echo "<tr bgcolor=#C0C0C0>">>/tmp/sync
echo "<td><p>`cat /tmp/time|sed -n $i\p`</p></td>">>/tmp/sync
echo "<td><p>`cat /tmp/Tsync|sed -n $i\p`</td>">>/tmp/sync
echo "<td><p>`cat /tmp/CL|sed -n $i\p`</td>">>/tmp/sync
echo "<td><p>`cat /tmp/CD|sed -n $i\p`</td>">>/tmp/sync
echo "</tr>">>/tmp/sync
done
echo "</table>" >>/tmp/sync
echo "</BODY>">>/tmp/sync
echo "</HTML>">>/tmp/sync
cat /tmp/sync|mail chittibabu.miriyala@ril.com
>/tmp/sync

No comments:

Post a Comment