The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pulling the first and last character/number from a string. LinuxRacr Shell Programming and Scripting 3 06-16-2008 09:08 PM
pulling scripts from unix to linux pjconfig UNIX for Dummies Questions & Answers 3 12-21-2005 06:10 PM
pulling a column from a file in ksh dangral Shell Programming and Scripting 8 01-13-2003 04:10 PM
pulling the following line from a file peter.herlihy UNIX for Dummies Questions & Answers 4 08-29-2002 09:09 PM
Pulling out fields from a file Saz UNIX for Advanced & Expert Users 2 09-30-2001 03:31 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-03-2009
MrKen MrKen is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 5
Pulling correct value from Mapfile

Hi all,

I am trying to pull one value from a Mapfile, but the result is that I am getting all the values. The Mapfile maps the host name from the Backup Server to the host name in our Network Monitoring Server, as shown here:

Mapfile
Code:
cat zabbixhosts 
Helpdesk-fd:Server_Helpdesk
Knowledge-fd:Server_Knowledge
BaculaServer-fd:Server_Bacula
Zabbix-fd:Server_Zabbix_1.4.2

The section of script that is failing is here:
Code:
client=$(tail -n 38 /var/lib/bacula/log | grep -m 1 -F "Client" | awk -F" " '{ print $2; }')
echo Client is $client
echo

# get hostname from the Hostsfile
ZABBIX_HOST=`grep "[$client]" $ZABBIX_HOSTSFILE`

if [ $? -eq 0 ]; then
        hostname=`echo "$ZABBIX_HOST" | cut -f2 -d:`
else
        hostname=""
fi
echo Hostname is $hostname
When I run that script the output is as follows:

Code:
./bacula2zabbix.sh 
Client is "BaculaServer-fd"

Hostname is Server_Helpdesk Server_Knowledge Server_Bacula Server_Zabbix_1.4.2

Any advice would be greatly appreciated.
Thanks.

Last edited by MrKen; 07-03-2009 at 02:16 AM.. Reason: Bad cut n paste
  #2 (permalink)  
Old 07-03-2009
prasperl prasperl is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 22
Did you initialize host file name?

I tried the same code snippet.The result is

Client is "BaculaServer-fd"

Hostname is Server_Bacula

ZABBIX_HOSTSFILE="path of map file"
I made two changes:

1.Initialized ZABBIX_HOSTSFILE to map file name


2.Removed []
grep "[$client]" $ZABBIX_HOSTSFILE
grep "$client" $ZABBIX_HOSTSFILE
  #3 (permalink)  
Old 07-03-2009
MrKen MrKen is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 5
Thanks for the quick reply.

When you say 'initialize' the host file name, I guess you mean do I have this:

ZABBIX_HOSTSFILE="/etc/bacula/zabbixhosts";

Yes, I have that already. That's why there is a result to the output in the first post.

When I remove the [ ] as suggested, the output is now:

Code:
Client is "BaculaServer-fd"

Hostname is
Any more ideas?
  #4 (permalink)  
Old 07-03-2009
reddybs reddybs is offline
Registered User
  
 

Join Date: Jan 2009
Location: Sriharikota-AP-India
Posts: 53
Code:
hostname=`grep $client zabbixhosts | cut -f2 -d:`
I tried to simulate your scenario in my linux machine. Assuming that your client variable contains <BaculaServer-fd >.
From the above code, variable hostname contains Server_Bacula if grep is successfull and will contain no value if it fails. Something like this....

Code:
$ cat zabbixhosts
Helpdesk-fd:Server_Helpdesk
Knowledge-fd:Server_Knowledge
BaculaServer-fd:Server_Bacula
Zabbix-fd:Server_Zabbix_1.4.2
 
$ echo $client
sai
 
$  hostname=`grep $client zabbixhosts | cut -f2 -d:` ; echo $hostname
 
$ client="BaculaServer-fd"
 
$ hostname=`grep $client zabbixhosts | cut -f2 -d:` ; echo $hostname
Server_Bacula
 
$ hostname=`grep -w $client zabbixhosts | cut -f2 -d:`; echo $hostname
Server_Bacula
  #5 (permalink)  
Old 07-03-2009
MrKen MrKen is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 5
reddybs, thanks for your reply.

If I change "[$client]" to $client, then I get no hostname output.
If I use -w with grep, then I get no hostname output.

So now I have this, but still the output is incorrect.
Code:
echo Client is $client
echo

# get hostname from the Hostsfile
hostname=`grep "[$client]" $ZABBIX_HOSTSFILE | cut -f2 -d:` ; echo $hostname
and the output:
Code:
Client is "BaculaServer-fd"

Server_Helpdesk Server_Knowledge Server_Bacula Server_Zabbix_1.4.2
Any more ideas?
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0