Bash string variable outputting incorrectly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash string variable outputting incorrectly
# 1  
Old 06-12-2012
Bash string variable outputting incorrectly

Hello All,

I am learning BASH scripting and I would appreciate any help with a small problem I am having...

I am writing a script that builds a simple hosts file for DNS reasons related to a piece of software called netdb by parsing another application's config files for IP's and their hostnames.


The script works correctly for all hosts except a relatively small group. What ends up happening is the string my script finds for a hostname ends up overwriting everything on the line that it is currently located on. I've narrowed my script down to only parse the config file that produces these problem strings after my script runs.

The erroneous output is as follows:

Code:
Number of addresses found is: 2
Number of DNS names found is: 2
Sync Check Success!
-----------------------------------------------------------

DEBUG: VARIABLE (IP) IS:  192.168.xxx.xxx
DEBUG: VARIABLE (host) IS:  Alderaan
-----------------------------------------------------------

DEBUG: VARIABLE (IP) IS:  192.168.xxx.xxx
DEBUG: VARIABLE (host) IS:  Bespin

Bespin .xxx.xxx
Alderaan .xxx.xxx

Number of addresses successfully converted is: 2

When the output should be:

Number of addresses found is: 2
Number of DNS names found is: 2
Sync Check Success!
-----------------------------------------------------------

DEBUG: VARIABLE (IP) IS:  192.168.xxx.xxx
DEBUG: VARIABLE (host) IS:  Alderaan
-----------------------------------------------------------

DEBUG: VARIABLE (IP) IS:  192.168.xxx.xxx
DEBUG: VARIABLE (host) IS:  Bespin

Bespin 192.168.xxx.xxx
Alderaan 192.168.xxx.xxx

Number of addresses successfully converted is: 2

here is my script code

Code:
#!/bin/sh

export IFS="
"
TotalAdrFound=`grep -E "address" -h xxx.cfg |cut -d ";" -f1|wc -l`
TotalDNSFound=`grep "A longer name" -h xxx.cfg|wc -l`

#Use grep to parase .cfg file and grab the ip number for each host
args=`grep "address" -h xxx.cfg| awk '{print $2;}'`


#Remove old list of DNS names.grep "address" -h *.cfg| cut -d "s" -f3 | cut -d ";" -f1 |
rm /tmp/addrs.netdb

#Use grep to parase .cfg file and grab the host name and output to addrs.netdb
sudo grep "A longer name" -h xxx.cfg| cut -d ";" -f1 |awk '{print $2;}' >> /tmp/addrs.netdb

TotalAdrSuccess=0

echo "Number of addresses found is: "$TotalAdrFound
echo "Number of DNS names found is: "$TotalDNSFound

#Check to see if every host has an  IP address.
if  [ "$TotalAdrFOUND"="$TotalDNSFound" ] ;
then
echo "Sync Check Success!"
else
echo "Sync Check FAILLED! There has been some type of error.  The Total Number " \
echo "     of Addresses found does not match the total number of DNS host names found."
fi

for IP in $args ;do
#Increment the line number for the sed command below
Line=$((Line+1))

#grab the host name for the IP
host=`sed "$Line!d" /tmp/addrs.netdb`

TotalAdrSuccess=$((TotalAdrSuccess+1))

#Some debug code for a select group of hosts.
echo "-----------------------------------------------------------\n"
echo "DEBUG: VARIABLE (IP) IS: " $IP 
echo "DEBUG: VARIABLE (host) IS: " $host

output="$IP $host\n$output"
done
echo "$output"
echo "Number of addresses sucessfully converted is: "$TotalAdrSuccess

Like I said above this works with all my other cfg files but this one. This cfg file is in the same format as all the other.

Last edited by jim mcnamara; 06-13-2012 at 12:01 AM.. Reason: code tags also for data...
# 2  
Old 06-13-2012
There are a few problematic lines in your script:


Code:
if  [ "$TotalAdrFOUND"="$TotalDNSFound" ] ;

i doubt that this works. correct should be (notice the spaces):
Code:
if  [ "$TotalAdrFOUND" = "$TotalDNSFound" ] ;

If i read the script source correctly the variables in question are both integers. You could also use integer comparation then (see man test for a complete description of available options)

Code:
if  [ $TotalAdrFOUND -eq $TotalDNSFound ] ;




Code:
Line=$((Line+1))

I am not sure if this works or not, but in any case this will definitely be correct (again, notice the spaces, they are necessary):

Code:
(( Line += 1 ))

A further tip: do NOT USE BACKTICKS! Backticks are an ancient device, which has some (quite intricate) shortcomings and are supported only for backwards compatibility. You use something like:

Code:
variable=`command`

and want the output of "command" to become the variables content. Use

Code:
variable="$(command)"

for this purpose, which has none of the shortcomings and all of the features of the above syntax.


One last tip: before using *any* variable, declare it at the beginning of your script. This offers the opportunity to document the variables contents, which helps greatly in maintaining your scripts, as well as to make sure all necessary variables are initialized with sensible values:

Code:
#!/bin/bash

# this is a sample script sketch

typeset    variable=""                # this is used for ....
typeset -i var_int=0                  # counter for loop cycling through something
typeset -i var_bool=0                 # processing flag, 0=process, 1=do not p.

<rest of your code>

I hope this helps.

bakunin

Last edited by bakunin; 06-13-2012 at 05:02 AM..
# 3  
Old 06-13-2012
Thanks for the help. Your pointers certainly have helped made my code run better.

But the real problem I am having is with this part of code

Code:
#Some debug code for a select group of hosts.
echo "-----------------------------------------------------------\n"
echo "DEBUG: VARIABLE (IP) IS: " $IP 
echo "DEBUG: VARIABLE (host) IS: " $host

output="$IP $host\n$output"
done
echo "$output"

Instead of outputting to the terminal on the echo command what is intended I get the following:

Code:
Hostname.10.2

when it should be:

Hostname 192.168.10.2

I have over 600 addresses and host names and it only does this weird output for 8 of them. I want to say its an overflow but I can't convince my self that is what is happening.

Thanks again!
# 4  
Old 06-13-2012
Quote:
Originally Posted by Wesley545
Thanks for the help. Your pointers certainly have helped made my code run better.
Well, probably they have helped some parts of your script run at all. In the meantime i have found some other syntactical errors and probable logic errors:


Quote:
Originally Posted by Wesley545
The script works correctly for all hosts except a relatively small group.
Sorry to say that, but more likely is the script produces the expected result out of pure chance with many hosts and isn't so lucky on the others.

First, have a look at these three lines:

Code:
TotalAdrFound=`grep -E "address" -h xxx.cfg |cut -d ";" -f1|wc -l`
TotalDNSFound=`grep "A longer name" -h xxx.cfg|wc -l`
<...>
if  [ "$TotalAdrFOUND"="$TotalDNSFound" ] ;

Variable names are case sensitive, therefore "$TotalAdrFOUND" will always be empty because it is never used nor given any value before. I suppose this to be a typing error, but this check (whatever it is for) can never have worked in the expected way.

Now for the probable cause of your visible problems:
Code:
for IP in $args ;do
#Increment the line number for the sed command below
Line=$((Line+1))

#grab the host name for the IP
host=`sed "$Line!d" /tmp/addrs.netdb`
<...>
done

To be honest this is by far the ....ahem.... most creatively phrased piece of code i ever saw. I needed several passes to even understand what it does.

If i get you correctly you have one or several IP addresses in $args. Now you want to take them one at a time in $IP and get the corresponding host names from a list stored in a file, reading it one by one, yes?

First, do not use a normal variable to store tabular data. This is what arrays are for. Now bash treats all variables in a pipeline local to this pipeline, so the following looks a little more clumsy than in ksh, but anyway:

Code:
(( index=1 ))
while read line ; do
     IP[$index]=$line
     (( index += 1 ))
done <<-EOF
     $(awk '{ /address/ {print $2};}' xxx.cfg)
EOF

This replaces the "$args" variable with an array with IP values. Test if they are correctly stored with the following:

Code:
(( index=1 ))
while [ $index -le ${#IP[*]} ] ; do
     echo IP[$index] is ${IP[$index]}
     (( index += 1 ))
done

Do the same with the host names. You just replace "address" with "A longer name" in the awk statement and replace the array name (like, say "DNS"). The logic stays the same.

Instead of this:

Code:
TotalAdrFound=`grep -E "address" -h xxx.cfg |cut -d ";" -f1|wc -l`
TotalDNSFound=`grep "A longer name" -h xxx.cfg|wc -l`
<...>
#Check to see if every host has an  IP address.
if  [ "$TotalAdrFOUND"="$TotalDNSFound" ] ;
then
<...>

you just use:

Code:
if  [ ${#IP[*]} -eq ${#DNS[*]} ] ; then
<...>

The construct "${#arrayname[*]}" is the number of elements stored in the array.

Now, instead of writing the DNS names into a temporary file first, then reading them back from there again, you simply do the following:

Code:
(( index=1 ))
while [ $index -le ${#IP[*]} ] ; do
     echo "-----------------------------------------------------------\n"
     echo "DEBUG: VARIABLE (IP) IS: " ${IP[$index]} 
     echo "DEBUG: VARIABLE (host) IS: " ${DNS[$index]}

     # or, alternatively, how you do your output:
     # echo "{IP[$index]}\t${DNS[$index]}"

     (( index += 1 ))
done

I hope this helps.

bakunin
# 5  
Old 06-13-2012
Quote:
To be honest this is by far the ....ahem.... most creatively phrased piece of code i ever saw. I needed several passes to even understand what it does
.

Haha well I'll take that as a compliment Smilie.

Anyways thanks for the help and putting up with my attempt at learning bash. Seems I have a ways to go still.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a string to variable in Bash shell

Hi All; I have 2 variable let's say $A and $B. These are actually some remotely executed command outputs. I captured these values in my local variables. Here is my problem. When I try to do some arithmetic operation with these variables, I receive an error message. Neither expr nor typeset -i... (3 Replies)
Discussion started by: Meacham12
3 Replies

2. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

3. Shell Programming and Scripting

bash: using a string variable in grep

Hi, I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg... grep 'pattern of several words' filename I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable: variable=$@ I... (2 Replies)
Discussion started by: adrian777uk
2 Replies

4. UNIX for Dummies Questions & Answers

bash variable string declaration

Hello, Why is this not working in a script? files="test.fsa" echo $files for file in $files do if then echo "$file does not exist." fi run a command done I get an error saying (3 Replies)
Discussion started by: verse123
3 Replies

5. Shell Programming and Scripting

BASH script outputting strange file formats

Hi I am very new to using BASH, but I have a problem with a piece of script that I have been working on. Basically the script goes through a mailbox file looking at particular aspects of the file, for example how many spamwords there are email address etc. It does this pretty well except for an... (13 Replies)
Discussion started by: 9aza
13 Replies

6. Shell Programming and Scripting

Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars) let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies

7. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

8. UNIX for Dummies Questions & Answers

outputting selected characters from within a variable

Hi all, if for example I had a variable containing the string 'hello', is the any way I can output, for example, the e and the 2nd l based on their position in the string not their character (in this case 2 and 4)? any general pointers in the right direction will be much appreciated, at... (3 Replies)
Discussion started by: skinnygav
3 Replies

9. Shell Programming and Scripting

Bash string variable manipulation

In a bash script I've set a variable that is the directory name of where an executable lives. the_dir=`dirname $which myscript` which equates to something like "/path/to/dir/bin" I need to cut that down to remove the "bin" so I now have "/path/to/dir/". This sounds easy but as a... (2 Replies)
Discussion started by: Witty
2 Replies

10. UNIX for Dummies Questions & Answers

PATH variable set incorrectly?

I've noted that in order to use commands like ifconfig, I have to prefix the commands with the directory. /etc/profile shows that the paths should be part of the PATH environment variable; any idea where the bug is? :confused: # /etc/profile # System wide environment and startup... (1 Reply)
Discussion started by: jon80
1 Replies
Login or Register to Ask a Question