Unix Blog !

May 20, 2008

Shell Script Comparing files …

Filed under: Shell Script — sriram003 @ 12:09 pm

Have a file (file1) which is like

host1
host2
host3
host4
the list goes on…………

Now I want the above lines in files to be compared with files under
/opt/new/*

File names under /opt/new are as below:
Dev
Prod
QA

And suppose host1 from file1 is found under Dev(file under /opt/new)
than it should write under a seperate file New-list as host1-DEV

Ans:

while read line; do echo ${line}-$(grep -l $line /opt/new/*); done < file1 > new-file

The above line reads from file1 and compares the list of hosts given under it with
all files under /opt/new/* and if the host on file matches on any of file.

Then it writes to a new file (new-file) as
host-filename, here host is the one listed in file1 and filename is
the file under which it found the host .

eg if it finds host1 under dev file (/opt/new)

It will write as host1-dev under new-file !

December 9, 2007

SSH Tips

Filed under: Shell Script, ssh, Unix Commands — sriram003 @ 6:41 am

Pointing to some SSH tips given in Linux Magazine:

$ssh -T user@hostname

If you connect to a host this way, it will disable pseudo-tty allocation
and a command like w will not show your connection. Better add
`bash -i’ , as below.
$ssh -T user@hostname /bin/bash -i

With the below command the IP address of the system you connect to wont be
logged into known_hosts file, usually its in /root/.ssh/known_hosts.

$ssh -o UserknownHostsFile=/dev/null -T user@hostname /bin/bash -i

August 15, 2007

Programming C under Unix

Filed under: C Program, Shell Script — sriram003 @ 6:11 pm

Writing a test Program in C (Unix):
====================================
[root@linuxbox C_Programs]# cat test.c
#include
main()
{

printf (“Hello Sriram!!\n”);

return 0;

}

Executing the above :
=====================
[root@linuxbox C_Programs]# gcc test.c

[root@linuxbox C_Programs]# ls
a.out test.c

[root@linuxbox C_Programs]# ./a.out
Hello Sriram!!

July 4, 2007

Shell Script – Restart Process if not found running

Filed under: Shell Script — sriram003 @ 12:55 pm

A script to Check if process is running and if not running
then start the process.

You can run this as a cron job in a 5/10 mins interval :

cat chk_if_process_running.sh
_______________________
# check daemon
ps -ef | grep -v grep | grep daemon
# if not found – equals to 1, start it
if [ $? -eq 1 ]
then
/sbin/init.d/daemon start
else
echo “eq 0 – daemon found – do nothing”
fi
________________________

April 29, 2007

Replace .htm with .html in 100’s of files inside a dir

Filed under: For Loop, Shell Script — sriram003 @ 1:39 pm

I needed to replace {.htm} files with {.html} extensions in
100’s of files in a directory :

Heres what I did :

[root@unixguy scripts]# ls
file1.htm file2.htm file3.htm

[root@unixguy scripts]# for list in `ls -1t *.htm*`
> do
> prefix=`echo $list | awk -F”\.” ‘{print $1}’`
> mv $list ${prefix}.html
> done

[root@unixguy scripts]# ls
file1.html file2.html file3.html

One more way of doing this :

[root@unixguy scripts]# ls
file1.html file2.html file3.html

[root@unixguy scripts]# ls *.html | awk -F ‘.’
‘{print “mv “$1”.html “$1″.htm”}’| csh

[root@unixguy scripts]# ls
file1.htm file2.htm file3.htm

Search and replace Shell Script

Filed under: Shell Script — sriram003 @ 1:00 pm

Had to parse 100’s of files in a loop and replace a word
called Bombay with a new word Mumbai

cat file1.dat
city name is Bombay and country is INDIA.

To:
cat file1.dat
city name is Mumbai and country is INDIA.

for file in /tmp/a /tmp/c /tmp/b ; do
sed 's/Bombay/MUMBAI/g' ${file} > ${file}.new
done

March 25, 2007

Bash Script

Filed under: Shell Script — sriram003 @ 1:10 pm

Heres a simple script I needed to execute if user found in
passwd file and if user not found in passwd file.

[root@unixguy scripts3]# cat verify.sh
#!/bin/bash -x

if grep $USER /home/sriram/scripts3/pass.txt;
then
#If user is found
perl script.pl;
else
#If user Not found
bash script.sh;
fi
[root@unixguy scripts3]#

Delete Files older than two Months from the Latest File

Filed under: Shell Script — sriram003 @ 12:02 pm

This script should Identify the Latest date file and delete files
which are older than 2 Months from latest date.

bash-3.00$ cat delet_old.sh

#!/bin/bash
touch -d “$(date -d “$(ls -lt | awk ‘NR==2{print $6,$7}’) 2 months ago”)” FILE_MARK &&
find . ! -newer FILE_MARK -exec rm -f {} \;

December 24, 2006

Failover routing – ISP switch

Filed under: Failover routing, Shell Script — sriram003 @ 7:58 am

a small shell script that ping a test ip and if it is
unreachable switch to the other gateway.
Schedule this script to run every minute or so from cron.

#!/bin/bash
GW1="192.168.10.254"
GW2="192.168.55.254"
TESTIP="192.71.220.10" # Any reliable Internet ip that responds to ping.
CURGW=`/sbin/route -n |awk '/^0.0.0.0/ {print $2 }'`

if ping -w2 -c3 $TESTIP >/dev/null 2>&1; then
echo "Active ISP is Ok."
else
if [ "$CURGW" = "$GW1" ]; then
NEWGW="$GW2"
else
NEWGW="$GW1"
fi
/sbin/route del default
/sbin/route add default gw $NEWGW
fi

This script requires that both your ISP’s are pre-configured and are
not messing with your routing table when connected/disconnected,
esp. PPP is know to be quite unpolite when dealing with routes.
You probably want some sort of test to see if the new path is
working as well. Other concerns may be iptables/tc reloading
and stuff like that.

October 14, 2006

Linux boot script

Filed under: Shell Script — sriram003 @ 6:08 pm

Some days back I had written a script to stop/start
application as linux boots.

Instead of adding the script manually , you could try this :

On Debian
update-rc.d
is the debian utility to install and remove
System-V style init script links.

#Create the script
Like this

# make it executable
chmod a+x

# copy the script to /etc/init.d and run the below command
update-rc.d script defaults

# To Erase:
update-rc.d -f script remove

http://wiki.linuxquestions.org/wiki/Update-rc.d

On Redhat
chkconfig
on RedHat Linux Systems:
Add this 2 lines at the beginning of script

# chkconfig: 345 85 15
# description: Description of program
This will add to runlevel 3,4 and 5 , Start order priority
will be set to 85 while Stop/Shutdown order will be set to 15

Set execute permission to script ===> chmod +x script name
Copy the script to /etc/rc.d/init.d/

When added to the boot process using the “chkconfig --add script-name
command the start order/priority will be set to 80 while the stop/shutdown
order will be set to 15. The process will be added to runlevels 3, 4 and 5.
This is enabled by generating links from the location of the script
(/etc/rc.d/init.d/) to the directory for the appropriate run level:
/etc/rc.d/rc#.d/. The file name in the run level directory will reflect if it
is used for boot (starts with an “S”) or shutdown (starts with a “K”)


Older Posts »

Blog at WordPress.com.