- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Need scripting help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:30 AM
тАО09-27-2004 02:30 AM
This a script was put out by one of my colleagues (honest) which did a sed and replaced an old hostname with a new host name for a server. However, it had to do this 9 times. This was done in individuial loops and the results >> to a new host file. The problem occured now in that we have 9 entries for each IP in /etc/hosts.
I have written a simple script to perform a check against each entry and then out put it to a new host file, however, I have found in some circumetances particualr lines on /etc/hosts are not picked up with a grep.
My script is attached. Any ideas how Icabn make sure it picks up each line when i perform `cat file | while read line` ??
Any ideas !?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:34 AM
тАО09-27-2004 02:34 AM
Re: Need scripting help
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:37 AM
тАО09-27-2004 02:37 AM
Re: Need scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:38 AM
тАО09-27-2004 02:38 AM
Re: Need scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:46 AM
тАО09-27-2004 02:46 AM
Re: Need scripting help
this is sort of debugging, but this will help you in knowing each line output.
Prashant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:49 AM
тАО09-27-2004 02:49 AM
Re: Need scripting help
-- your script ---
#!/usr/bin/sh
## Debugging
## set -x
LOG=/var/host.log
DUFF=/usr8/hosts.duff
GOOD=/usr8/hosts.good
cp /etc/hosts $DUFF
touch $GOOD
cat $DUFF | while read line
do
sleep 1
if
[ `grep "$line" $DUFF | wc -l` -gt 1 ] && [ `grep "$line" $GOOD | wc -l
` = 0 ]
then
echo "Duplicate entry in host file" >> $LOG
echo "$line" >> $GOOD
else
echo "Found the entry in the good host file, loop has completed." >> $LOG
fi
done
cp $GOOD /etc/hosts
chmod 444 /etc/hosts
Your are using ir-required logic's here. Good file will not be containing any entries why you are checkign there with duff file there??
There is no action done to change the hostname there in your script.
To change your hostname use sed / awk / perl program there.
IF you want to get duplication on /etc/hosts file then,
cp /etc/hosts /tmp/hosts
grep -vf /etc/hosts /tmp/hosts
will give duplication entries there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 02:50 AM
тАО09-27-2004 02:50 AM
Re: Need scripting help
` = 0 ]
may read "`grep "$line" $GOOD | wc -l
` -eq 0 ]
Regards
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 03:01 AM
тАО09-27-2004 03:01 AM
Re: Need scripting help
My check for good file, is when it finds a line the next time it finds a duplicate entry the line will be in the good file so it will not echo $line again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 03:04 AM
тАО09-27-2004 03:04 AM
Re: Need scripting help
Sorry, Im quite happy with the content of the script,as it does what it is supposed to.
But some entries in hosts are not picked up by grep "$line" /etc/hosts and I wondered why ? OR if anyone knew a better method. I have tried awk too on the same line and it cant find it either....
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 03:27 AM
тАО09-27-2004 03:27 AM
Re: Need scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 04:09 AM
тАО09-27-2004 04:09 AM
Re: Need scripting help
the problem is because of if loop check there as,
if
[ `grep "$line" $DUFF | wc -l` -gt 1 ] && [ `grep "$line" $GOOD | wc -l
` = 0 ]
while checking number then use -eq or -ne etc there as,
if
[ `grep "$line" $DUFF | wc -l` -gt 1 ] && [ `grep "$line" $GOOD | wc -l
` -eq 0 ]
but any way it will be checking that,
1> line contains in the file more than once or line must not be there in good file.
so that it will add the contents to good file there.
your script is not checking duplicated liens which they located two lines later, etc there.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-27-2004 04:39 AM
тАО09-27-2004 04:39 AM
SolutionTry this script,
#!/usr/bin/sh
# Debugging mode
set -x
#### Log files ######
LOG=/var/host.log
DUFF=/usr8/hosts.duff
GOOD=/usr8/hosts.good
# hosts files copy #
cp /etc/hosts $DUFF
cp /etc/hosts /etc/hosts.def
# Create a new file if exits delete and create
[[ -f $GOOD ]] && rm -f $GOOD
touch $GOOD
# Duplication check
cat $DUFF | while read line
do
if [[ `grep "$line" $DUFF | wc -l` -gt 1 && `grep "$line" $GOOD | wc -l` -eq 0 ]]
then
echo "Duplicate entry $line in host file" >> $LOG
echo "$line" >> $GOOD
elif [[ `grep "$line" $DUFF | wc -l` -eq 1 && `grep "$line" $GOOD | wc -l` -eq 0 ]]
then
echo "$line" >> $GOOD
else
echo "Found the entry in the good host file, loop has completed." >> $LOG
fi
done
# Copy to hosts file
cp $GOOD /etc/hosts
chmod 444 /etc/hosts
It will work now.
Note: While your are operating on system files as like /etc/hosts without proper check don't operate there as,
cp $GOOD /etc/hosts
chmod 444 /etc/hosts
it will make that $GOOD files to be copied to /etc/hosts so that no entries will be there in /etc/hosts file too.
In My script I am using as,
cp /etc/hosts /etc/hosts.def
so that default will be there in /etc/hosts.def to get it for ever in future :-)
HTH.