Category Archives: Unix Shell Scripting

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

Difference between Unix and Linux

Whether you are an expert in Unix/Linux environment or a complete newbie into Black & White screen world, it’s some time hard to answer this basic question. If you have that feeling, please read on!

Before we understand the difference between Unix & Linux, let me share the origin of “Uni”x name.

Before UNIX came into the Industry, there used be an Operating Sytem (OS) called “Multics” (Multiplexed Information and Computing Service). If you observe the word “Multi”cs, there is a little word inside it called “Multi”, which means more than one. That was exactly this MULTICS operating system used to do; It tried to balance all operating system features such as response time, user interfaces, etc.

But, our UNIX Inventors – Brain kernighan and Ken Thompson @AT&T Bell Laboratories in early 1970’s – wanted to have an operating system that delivers faster response time for their academic project; Unfortunately, MULTICS was not designed for this single purpose of giving faster response time. Hence, these inventors started writing a platform with concentration on only one feature, faster response time. Hence, the name “Uni”x.

If you ask me the authenticity of the above 2 paragraphs, I might be wrong! But it at least helped me to remember the “Unix” meaning permanently.

To add further, 1970’s was the same time, Dennis Ritchie invented “C” programming language. So, Unix has got 90% of it code developed in “C” language and 10% in machine specific assembly code.

After their academic project, the source code for UNIX was kept open, which eventually received wider acceptance from Industry leaders such as IBM, SCO, SUN, Berkley, HP, etc. From then, each vendor/industry leader developed its own operating system.

Few examples of such UNIX operating systems: IBM AIX, SUN Solaris, HP-Ux, SCO

Then, What is LINUX?
Linux is a Unix clone written from SCRATCH by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. Taking the first name – Linux- of this inventor, this operating system has been named as “Linu”x.  In my opinion, Linux included Unix functionality PLUS GUI capabilities that lacked in Unix platforms. Probably, to take out the market share from Windows GUI platform.

Few example LINUX operating systems: Debian, Ubuntu, Redhat, etc

Hope this helps. Feel free to comment if any queries/changes you think are needed.
P.S: The above post is for Knowledge purpose only, neither intended to violate any copyrights nor to take away someone else’s
credit on our name. If you think, any such violation, please email me at nravi3@gmail.com. We will include the credits accordingly.

P.S: If you are interested in sharing your knowledge, please email your articles to nravi3@gmail.com

Happy Reading & Sharing!

Thanks,
Ravi