Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Thursday, November 6, 2014

How to Handle the Line with spaces in Shell for loop


One of the error that was faced while doing automation in Bash,ksh is that
when you use the for loop to loop a records that have spaces ,then it takes each word in a line and loops it.

This is due to the Setting of the Variable IFS

     
man bash


   IFS    The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the  read  builtin  command.
              The default value is ‘‘<space><tab><newline>’’.

As it states above , the default IFS make it to happen that way.

How to overcome it, is by using the below where you can say to consider only the New Line as Internal Field separator.

  IFS=$'\n' 
  for BLIST in `cat $FileList`
   do
    echo "$BLIST"
    .
    .
    .
   done


Sample File :

  $cat FileList 
      A B C
      AA FF EE
      DD SS RR

Friday, June 14, 2013

ssh Password Less Login

Follow the below steps to perform the ssh passwordless login,

1.Login to the Server and run the below to generate the Key pair.

Sri> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sri/.ssh/id_rsa):
Created directory '/home/sri/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sri/.ssh/id_rsa.
Your public key has been saved in /home/sri/.ssh/id_rsa.pub.
The key fingerprint is:
7y:74:10:98:25:06:17:d7:49:b7:u2:46:2i:jc:10:92 sri@beetle
The key's randomart image is:
+--[ RSA 2048]----+
|          ++B=oo=|
|         oE+..+.*|
|        . o  o = |
|         . o  . o|
|        S + .  . |
|         . o     |
|                 |
|                 |
|                 |
+-----------------+

2. The Private Keys gets genenrated .The Public Key gets created in the "/home/sri/.ssh/id_rsa.pub". 

Sri> cat /home/sri/.ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAouNqZ7+sK0P2zjf+y6pM1CY4jCZTD+CVzfjYyiAB7VhpIkn70MiPiNcbbrNCmIr4v+9U64Mm4ioAz9V4WJOhZ6+1/QLMhPpIhZcq7D1wfc26xU/b4moyzzygX8WaNd2D1XRX5JZMEcz/ir4C6wTFG9lL9Y9iKkKK2/sJHYGCDHJemxsw9wY4gH1fvrfdY/1M/rG4OOMrHsp9s6PB+1v+G7lEIHozDkPBDZx0ypSM4YNKZR6ddPt+GpcWU0RlS9X0eMeFljtWawmr1W90p/KeqwxQaVhl6YVZnQw1dJ0T5YUQIEhQcHJdJpbOfkN962D8dGJYGnjnTN2vJw83xoB/CQ== sri@beetle

3.To perform the Login without the Password copy the Public Key content (output is One line ) to the ".ssh/authorized_keys" on the remote System .
   The copy to the remote system can be done using scp or you can use the ssh as below

Sri> ssh remote -l sri "echo ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAouNqZ7+sK0P2zjf+y6pM1CY4jCZTD+CVzfjYyiAB7VhpIkn70MiPiNcbbrNCmIr4v+9U64Mm4ioAz9V4WJOhZ6+1/QLMhPpIhZcq7D1wfc26xU/b4moyzzygX8WaNd2D1XRX5JZMEcz/ir4C6wTFG9lL9Y9iKkKK2/sJHYGCDHJemxsw9wY4gH1fvrfdY/1M/rG4OOMrHsp9s6PB+1v+G7lEIHozDkPBDZx0ypSM4YNKZR6ddPt+GpcWU0RlS9X0eMeFljtWawmr1W90p/KeqwxQaVhl6YVZnQw1dJ0T5YUQIEhQcHJdJpbOfkN962D8dGJYGnjnTN2vJw83xoB/CQ== sri@beetle  >>~/.ssh/authorized_keys"

Enter the password when its prompted and the authorised_keys of Remote system is updated and now you can login without Password to the remote system.

Monday, February 4, 2013

Testing : sh -u : Unbound variable

Today came across a good feature "-u" in shell. This will make the script to error when it comes across a undefined variable. If otherwise ,the undefined variable will expanded to nothing,

Running the script with "-u" option checks for the variables is not defined then throws the error "unbound variable"

Lets do it with a example,


$ cat t.sh
TEST=""
echo "Test"
echo $TEST

Running the script with "-u" option,

$ sh -u t.sh
Test



Now removing the Variable assignment and running the script,

$ cat t.sh
echo "Test"
echo $TEST


$ sh -u t.sh
Test
t.sh: line 5: TEST: unbound variable

Monday, January 30, 2012

Range values {1..n} in FOR Loop doesn't work in AIX,HP,SUN ...

The FOR Loop with range values is specially for the BASH shell.
So when you get to other OS ,if the BASH is not set then you will not be getting the expected error.


Code :

echo Shell:: $SHELL
for range in {1..3}
do
echo "Range: $range"
done;

OUTPUT :



Linux
AIX
SUN
  Shell::/bin/ksh 
  Range: 1
  Range: 2
  Range: 3
 Shell::/bin/ksh
 Range: {1..3}
 Shell::/bin/ksh
 Range: {1..3}



How to overcome this issue :

Add the "#!/bin/bash" . (find the path of the bash in other os and add it in the script.)

which bash
/usr/bin/bash

Code :

#!/usr/bin/bash
echo Shell::$SHELL
for range in {1..3}
do
echo "Range: $range"
done;


OUTPUT : AIX 

Shell::/bin/ksh
Range: 1
Range: 2
Range: 3

Friday, December 9, 2011

Running Sql in a single file


More often we get to the scenario where 

100's of files in a directory or a list of files mentioned in a txt file and we need to run them.

The way that comes to mind spontanesously is to run one by one, but how about run it in a single shot where you can know what you are running and also can spool it ,so that you know which one errored.

Below are the two approaches where you can create a single file and run it in the sqlplus with spool.


For files in a directory

  • Get to the directory where files are (example : /home/srini/function )
  • In the command prompt run the following 
  • >runall.sql
    for i in `ls -1`
    do
    echo "PROMPT Running File $i ...." >>runall.sql
    echo "@@$i" >>runall.sql
    ## echo "@$PWD$i" >>runall.sql -- For having the full path of the sql
    done;
  • Run the runall.sql in the Sqlplus .

For files in a text File

  • Go to the location of the file having the list of all .sql (example : example : /home/srini/function.txt )
  • In the command prompt run the following 
>runall.sql
for i in `cat package.dat`
do
echo "PROMPT Running File $i . ..." >>runall.sql
echo "@@$i" >>runall.sql
## echo "@$PWD$i" >>runall.sql -- For having the full path of the sql
done;
  • Copy this runall.sql to the location where you have all the .sql and run it in sqlplus

Wednesday, June 8, 2011

Variable Scope - PIPE and WHILE LOOP


The article discusses about the scope of a variable in a While loop (when using PIPE):

Code:

Test="ONE"
echo $Test|while read Test
do
if [ "$Test" = "ONE" ]
then
Test="Two"
echo "INSIDE LOOP :TEST= $Test"
break;
fi
done;
echo "OUTSIDE LOOP :TEST=$Test"

OUTPUT :
INSIDE LOOP :TEST= Two
OUTSIDE LOOP :TEST=ONE

The expected output is "Two" ,but it came as "ONE".
 This is due to the PIPE "|" used in "echo $Test|while read Test".
The PIPE runs the WHILE in a subshell which makes the scope of the WHILE Loop Local
resulting in the OUTPUT "Two".

How to resolve this ?

Code :

Test="ONE"
echo $Test>/tmp/sri.txt
while read Test
do
if [ "$Test" = "ONE" ]
then
Test="Two"
echo "INSIDE LOOP :TEST= $Test"
break;
fi
done </tmp/sri.txt
echo "OUTSIDE LOOP :TEST=$Test"

OUTPUT:
INSIDE LOOP :TEST= Two
OUTSIDE LOOP :TEST=Two

To resolve this, one of the workaround is to gat the date in to file and input the file to the While loop.

Tuesday, March 8, 2011

Check errors on Alert log based on Date.



This script is for the DBA whose alertlog is huge , may be a year old history in it.This script will report the errors that occurred yesterday.

Script :


Today=`date +"%a %b %d"`
Yesterday=`date +"%a %b %d" -d "YESTERDAY"`
sed -ne "/$Yesterday /,/$Today/p" alert_envrtl.log >alert_temp
cat alert_temp|egrep -i "ORA-|SP2-"
rm alert_temp

Also you can add other errors that you like to grep apart from ORA- & SP2-

Note : The alert log should not be older than one year.


Tuesday, November 9, 2010

SQLPATH - variable for DBA

Configuring the SQLPATH will make make the job of running the".sql" files more easier.

SQLPATH is a variable which when set to a directory, The SQL*PLUS will search for the files with this directory and sub directory and runs the file.

Also we can set a file -> login.sql , it will be run by default every time you login through SQL*PLUS.
Below is a simple login.sql configuration.
login.sql


SET LINES 200
SET PAGES 10000
SET LONG 100000
SET SQLPROMPT "_USER'@'_CONNECT_IDENTIFIER>"
DEFINE _EDITOR=vi
COL OBJECT_NAME FOR A40


As a DBA we always do certain formatting before working on the SQL Session and if  this is set in login.sql then Oracle will take care of running it all the time we login. :-)

For this just set the following in your Linux/Unix profile ,

SQLPATH=/home/sri/sql:/home/sri/bin  ; export SQLPATH

For Windows configuration steps :


The information is documented clearly in the following link,
http://download.oracle.com/docs/cd/B28359_01/server.111/b31189/ch2.htm

after setting up the SQLPATH, now you can start making the sql scripts for the regular monitor queries like sga.sql , temp_usage.sql etc, in the SQLPATH directory and you can run it anytime in the SQL session by


SRI@sridb>!pwd
/home/sri

SRI@sridb>@sga.sql

SQLPATH will help a lot :-)