Monthly Archives: November, 2014

IBM Informix pops up big in China!

IBM Informix becomes THE RDBMS for the government of the Popular Republic of China! See the details of this game changer IBM deal in IIUG website:

http://www.iiug.org/Insider/insider_oct14.php#H%2E1

 

Thanks,
Ravi

Shell Script: Usage of Arrays

#!/bin/bash

if [ “$#” -ne “9” ]
then
echo “Pass 9 CMD LINE arguments …”
exit
fi

arg[1]=$1
arg[2]=$2
arg[3]=$3
arg[4]=$4
arg[5]=$5
arg[6]=$6
arg[7]=$7
arg[8]=$8
arg[9]=$9

for i in {1..9}
do
echo “${arg[i]}”
done

 

Thanks,
Ravi

Shell Script: Usage of Arrays

#!/usr/local/bin/bash
area[11]=23
area[13]=37
area[51]=test

echo -n “area[11] = ”
echo ${area[11]} # {curly brackets} needed

echo -n “area[13] = ”
echo ${area[13]}

# Contents of uninitialized array variable print blank.
echo -n “area[43] = ”
echo ${area[43]}

#sum of two array variables assigned to third
area[5]=`expr ${area[11]} + ${area[13]}`
echo “area[5] = area[11] + area[13]”
echo -n “area[5] = ”
echo ${area[5]}
****

Thanks,
Ravi

Shell Script: Running parallel process in subshells

#!/bin/sh

cat   list1  list2  list3  | sort |    uniq  >  list123  &

cat   list4  list5  list6  | sort |    uniq  >  list456  &

wait                   #Doen’t execute the next command until subshells finish

diff        list123     list456

Note1: If no argument is specified, wait waits until all processes (started with &) of the current shell have completed, and reports on abnormal terminations.

Note2:  Because the wait() system call must be executed in the parent process, the shell itself executes wait without creating a new process.
****

Happy Reading & Sharing!

Thanks,
Ravi

Shell Script: Usage of Function inside shell script

#!/bin/sh

# file.sh: a sample shell script to demonstrate the concept of Bash shell functions

# define usage function

usage()

{

echo “Usage: $0 filename“; exit 1

}

# define is_file_exists function

is_file_exists()

{

f=”$1”

if [ -f “$f” ]

then

return 0

else

return 1

fi

}

# invoke  usage

if [ $# -ne 1 ]

then

usage

fi

# Invoke is_file_exists

#if ( is_file_exists “$1” )

is_file_exists “$1”

if [ $? -eq 0 ]

then

echo “File found”

else

echo “File not found”

fi
****

Thanks,
Ravi

Script: Server Alive Check Script

About Script: The below script checks whether the given servers in mac_list.sh are alive or not. Send report through email.

*****

#!/bin/sh

HOSTS=`cat ./mac_list.sh`

export TIMESTAMP=`date +%a%d%b%Y-%X%Z`

export LOGOUT=/tmp/asdf.$TIMESTAMP.log

export MAIL_TO=nravi3@gmail.com

echo $HOSTS

for myHost in $HOSTS

do

echo $myHost

ping $myHost -n 3 >/dev/null

if [ $? -eq 0 ]

then

echo “Bingo, Ping is successful for $myHost” >> $LOGOUT

echo “Successful”

else

echo “Oops! Ping failed for $myHost” >> $LOGOUT

echo “Not Successful”

fi

done

mailx -s “Hosts ping is completed.” $MAIL_TO < $LOGOUT

if [ $? -eq 0 ]

then

echo “Email was successfully sent”

else

echo “Email sent is failed”

fi

$ cat mac_list

machine1.us.client.net

machine2.emea.client.net
****

Thanks,
Ravi

Script: Global Search and Replace

#!/bin/sh

#set -x

N=`basename $0`

if [ $#  -ne 3 ]

then

echo “$N: Usage”

echo ”        $N <Search Path> <Search String> <Replace String> “

exit 0

fi

dir_path=$1; str_search=$2; str_replace=$3

for file in $(find $dir_path -type f)

do

sed “s/$str_search/$str_replace/g” $file >/tmp/temp$$

diff $file /tmp/temp$$ >/dev/null

if [ “$?” -ne “0” ]

then

echo “$file is replaced”

cp /tmp/temp$$ $file

fi

rm -rf /tmp/temp$$

done

 

Thanks,
Ravi

Script: Global Search for a string and Report

#!/bin/sh

#set -x

N=`basename $0`

if [ $#  -ne 2 ]

then

echo “$N: Usage”

echo ”        $N <Search Path> <Search String>“

exit 0

fi

dir_path=$1; str_search=$2;

for file in $(find $dir_path -type f  2>/dev/null)

do

grep -i     $str_search     $file    2>/dev/null >/dev/null

if [ “$?” -eq “0” ]

then

echo  “$file”  >>/tmp/search_ravi$$

fi

done
****

Thanks,
Ravi

Shell Script: File Name replacement (using SED)

Problem:

We have an environment named MX11, where we are planning to change the environment name with Q1_MX11.
Because of this, all files inside MX11 directory needs to be renamed. I need to change all the files or directories that have the name MX11 to Q1_MX11. please help me with a script to change all the directories or files under home\MX11.

Solution:
********
#!/bin/bash

SEARCH_DIR=/home/MX11
SEARCH_STR=MX11
REPLACE_STR=Q1_MX11

for fname in `find $SEARCH_DIR -name “*$SEARCH_STR*”`
do

#echo $fname
nfname=`echo $fname | sed “s/$SEARCH_STR/$REPLACE_STR/g”`
#echo $nfname
echo “$fname is changed to $nfname”
sleep 1

done

 

Thanks,
Ravi