LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash read value function (https://www.linuxquestions.org/questions/programming-9/bash-read-value-function-616594/)

mcandy 01-27-2008 05:17 AM

bash read value function
 
Hello Cracks !
Could someone help me with a bash command, I am not firm in this.
So here is what I need:
my application settings are configured by an config.xml file, so I need for a check reason to read out the ip address specified in this xml.
Looks like follows:
<dhcpModeItf0>0</dhcpModeItf0>
<IpAddrItf0>10.0.0.4</IpAddrItf0>
<IpNetmaskItf0>255.255.0.0</IpNetmaskItf0><firstNtpName>10.0.0.1</firstNtpName>

so I need to specify a variable like a=`command to read out the(sed,awk,grep,find) "IpAddrItf0 Ipaddress" `
the result should be the content of <IpAddrItf0> filed = 10.0.0.4 in my example.

Any idea will be appreciated !
Thanks in advance !

/bin/bash 01-27-2008 05:28 AM

$ cat file
<dhcpModeItf0>0</dhcpModeItf0>
<IpAddrItf0>10.0.0.4</IpAddrItf0>
<IpNetmaskItf0>255.255.0.0</IpNetmaskItf0><firstNtpName>10.0.0.1</firstNtpName>

$ cat doit
#!/bin/bash
IP=$(grep IpAddrItf0 file|sed -e 's/<[^>]*>//g')
echo "IP = $IP"

$ ./doit
IP = 10.0.0.4

mcandy 01-27-2008 02:05 PM

wow, easy if I read it, I understand, many thanks !!!!
 
Quote:

Originally Posted by /bin/bash (Post 3036483)
$ cat file
<dhcpModeItf0>0</dhcpModeItf0>
<IpAddrItf0>10.0.0.4</IpAddrItf0>
<IpNetmaskItf0>255.255.0.0</IpNetmaskItf0><firstNtpName>10.0.0.1</firstNtpName>

$ cat doit
#!/bin/bash
IP=$(grep IpAddrItf0 file|sed -e 's/<[^>]*>//g')
echo "IP = $IP"

$ ./doit
IP = 10.0.0.4

Thanks Cracks !!!!!

mcandy 02-01-2008 03:07 PM

hi and thanks again ! Still a little problem in the search function !
 
Quote:

Originally Posted by mcandy (Post 3036810)
Thanks Cracks !!!!!

Hi Cracks, (bash)
first of all, thanks for the idea, really good, but I have a little problem with this proposal.
in my shell it doesnt work always well.
If my *.xml file is structured, line by line this proposal below works well, for e.g. the sequence in xml is looking like:

<IpAddr_1>10.0.0.1</IpAddr_1>
<IpAddr_2>10.0.0.2</IpAddr_2>
<IpAddr_3>10.0.0.3</IpAddr_3>
<IpAddr_4>10.0.0.4</IpAddr_4>

all is working fine, line by line, but I have an xml with different structures looking some times like:
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>

in this line the comand

IP=$(grep IpAddrItf0 file|sed -e 's/<[^>]*>//g')

displays me all content of all tags together, like :
1.0.0.110.0.0.210.0.0.310.0.0.4

so as I have this different structures I need a command which searches really for this particular tag and reads out ONLY its content.
Any idea??

Thank you in advance

/bin/bash 02-01-2008 07:22 PM

$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.1</IpAddr_5>
<IpAddr_6>10.0.2.2</IpAddr_6>
<IpAddr_7>10.0.2.3</IpAddr_7>
<IpAddr_8>10.0.2.4</IpAddr_8>

$ cat doit
#!/bin/bash
IParray=( $(grep IpAddr_ file|sed 's/<\(IpAddr_.*>\)\(.*\)<\/\1/ \2/g') )
echo "Found ${#IParray[@]} IP Addresses"
echo "IParray = ${IParray[@]}"

$ ./doit
Found 8 IP Addresses
IParray = 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.2.1 10.0.2.2 10.0.2.3 10.0.2.4


You would access the individual IP Addresses using IParray:
IP0=${IParray[0]}

KenJackson 02-02-2008 12:42 AM

$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.5</IpAddr_5>
<IpAddr_6>10.0.2.6</IpAddr_6>
<IpAddr_7>10.0.2.7</IpAddr_7>
<IpAddr_8>10.0.2.8</IpAddr_8>

$cat doit
#!/bin/bash
sed -ne 's:<ipaddr_[0-9]*>\([^<]*\)</ipaddr_[0-9]*>:\1\
:pgi' "$1" | grep -v '^$'

$ ./doit file
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.2.5
10.0.2.6
10.0.2.7
10.0.2.8

mcandy 02-02-2008 03:10 AM

thanks, I'll try this on monday
 
-------
thanks, I'll try this on monday
-------

Quote:

Originally Posted by /bin/bash (Post 3043083)
$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.1</IpAddr_5>
<IpAddr_6>10.0.2.2</IpAddr_6>
<IpAddr_7>10.0.2.3</IpAddr_7>
<IpAddr_8>10.0.2.4</IpAddr_8>

$ cat doit
#!/bin/bash
IParray=( $(grep IpAddr_ file|sed 's/<\(IpAddr_.*>\)\(.*\)<\/\1/ \2/g') )
echo "Found ${#IParray[@]} IP Addresses"
echo "IParray = ${IParray[@]}"

$ ./doit
Found 8 IP Addresses
IParray = 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 10.0.2.1 10.0.2.2 10.0.2.3 10.0.2.4


You would access the individual IP Addresses using IParray:
IP0=${IParray[0]}


mcandy 02-02-2008 03:11 AM

thanks, I'll try this on monday
 
thanky you very much, I will try this on monday, keep you ionformed.


Quote:

Originally Posted by KenJackson (Post 3043317)
$ cat file
<IpAddr_1>10.0.0.1</IpAddr_1><IpAddr_2>10.0.0.2</IpAddr_2><IpAddr_3>10.0.0.3</IpAddr_3><IpAddr_4>10.0.0.4</IpAddr_4>
<IpAddr_5>10.0.2.5</IpAddr_5>
<IpAddr_6>10.0.2.6</IpAddr_6>
<IpAddr_7>10.0.2.7</IpAddr_7>
<IpAddr_8>10.0.2.8</IpAddr_8>

$cat doit
#!/bin/bash
sed -ne 's:<ipaddr_[0-9]*>\([^<]*\)</ipaddr_[0-9]*>:\1\
:pgi' "$1" | grep -v '^$'

$ ./doit file
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.2.5
10.0.2.6
10.0.2.7
10.0.2.8


mcandy 02-10-2008 10:01 AM

hi cracks, I tried it, doesn't work, here again a problemdescription
 
Quote:

Originally Posted by mcandy (Post 3043372)
thanky you very much, I will try this on monday, keep you ionformed.

Hi Cracks,
to explain more detailed my problem, here below a part of my config.xml file, note, thise file is buildet exactly like below, so the way the colums are displayd is structure like!
here is what i would like to have done by a routine:
1.) find the part in the file starting with the tag "<BBGateway>"
2.) read out the value of the tag "<gatewayIP>?</gatewayIP>, note, this is the only one value i need and this tag could be in this <BBGateway> .... </BBGateway> - structure in various positions.


here my example structure:

<AAGatewayTable>

<BBGateway><parm1>010203</parm1><parm2>0.0.0.0</parm2>
<netmaskDest>0.0.0.0</netmaskDest><gatewayIp>10.10.10.10</gatewayIp>
</BBGateway><GWLastChangeCounter>1</GWLastChangeCounter></AAGatewayTable>

<blabla>..</blabla><blabla>..</blabla><blabla>..</blabla>
<blabla>..</blabla>

<blabla>..</blabla>



I don't know how to manage this, it can not be so difficult to read this out, if I could get a solution, I could use it for more needed tags in this file, as the structure type is always the same.
Thanks Guys,
I appreciate any help !

ghostdog74 02-10-2008 10:30 AM

what have you got so far ? you had some possible ways to solve your problem, so you should have come up with something by now

KenJackson 02-10-2008 05:15 PM

Code:

#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
my $xmlfile = shift @ARGV;
exit if (not defined $xmlfile or $xmlfile eq '');
my $xml = XMLin($xmlfile);
#print Dumper($xml);
print "IP = $xml->{BBGateway}->{gatewayIp}\n";

Note: You must have perl and perl-XML-Simple installed.

mcandy 02-15-2008 11:47 AM

hey Kan, thanks it works well !
 
Thank You Kan !



Quote:

Originally Posted by KenJackson (Post 3052845)
Code:

#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;
my $xmlfile = shift @ARGV;
exit if (not defined $xmlfile or $xmlfile eq '');
my $xml = XMLin($xmlfile);
#print Dumper($xml);
print "IP = $xml->{BBGateway}->{gatewayIp}\n";

Note: You must have perl and perl-XML-Simple installed.



All times are GMT -5. The time now is 02:49 AM.