?
Solved

bash variable limitation?

Posted on 2014-07-10
12
Medium Priority
?
248 Views
Last Modified: 2014-07-11
This works perfectly from the command line but when I run it as a script, it lists all of the directory contents I am currently running it from. What is causing this to happen?

#!/bin/bash

COUNT=0
LIST=$( traceroute -n xxx.com | awk '{if(NR>1)print $2}')
for IP in $LIST; do
COUNT=$(($COUNT + 1))

ip=$IP
level=$COUNT
echo "$IP" - "$COUNT"
done

# ./test
192.168.1.1 - 1
xx.xx.xx.7 - 2
xx.xx.xx.1 - 3
xx.xx.xx.33 - 4
xx.xx.xx.21 - 5
xx.xx.xx.9 - 6
xx.xx.xx.x4 - 7
xx.xx.xx.xx - 8
xx.xx.xx.9 - 9
TZ - 10                  Things get weird here, the directory contents are now listed
servreinfo - 11
dropchar - 12
lockbox - 13
log - 14
resolve.cf - 15
resolv.cf.2 - 16
storage - 17
lists - 18
testscript.sh - 19
state4 - 20
info - 21
test - 22
rruid - 23
limiter.sh - 24
xx.xx.xx.xx - 25    Finally, the end IP I was trying to traceroute
0
Comment
Question by:projects
  • 6
  • 6
12 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 40189486
What is in $LIST?  Does it contain any * characters?
0
 

Author Comment

by:projects
ID: 40189499
It's the results but listed over and over and over again with the count changing positions here and there.
0
 
LVL 85

Expert Comment

by:ozo
ID: 40189514
Did $( traceroute -n xxx.com | awk '{if(NR>1)print $2}') produce any "*" characters?

If so, try
set -f
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:projects
ID: 40189622
# traceroute -n xxx.com | awk '{if(NR>1)print $2}'
192.168.1.1
xx.xx.xx.7
xx.xx.xx.1
xx.xx.xx.xx
xx.xx.xx.21
xx.xx.xx.9
xx.xx.xx.xx4
xx.xx.xx.x8
xx.xx.xx.x0
xx.xx.xx.xx
xx.x.x.x
129.250.5.69
*
*
*
129.250.2.147
83.231.213.38
141.0.171.250
141.0.173.173

From within the script, same as before, simply too much to list. Repeats over and over and some random stuff including the directory.
0
 
LVL 85

Expert Comment

by:ozo
ID: 40189817
So the *'s would be expanded to files matching that pattern.
I'd suggest either removing them, or turning off globbing with
set -f
0
 

Author Comment

by:projects
ID: 40189822
Can you expand on that please, I'm not sure I'm understanding that.
0
 
LVL 85

Accepted Solution

by:
ozo earned 1060 total points
ID: 40189839
#!/bin/bash

COUNT=0
LIST=$( traceroute -n xxx.com | awk '{if(NR>1)print $2}')
set -f
for IP in $LIST; do
COUNT=$(($COUNT + 1))

ip=$IP
level=$COUNT
echo "$IP" - "$COUNT"
done
0
 

Author Comment

by:projects
ID: 40191323
Inside the script, still only shows 7 hops.
0
 
LVL 85

Expert Comment

by:ozo
ID: 40191359
Since your script originally showed 25, that sounds like an improvement.
0
 

Author Comment

by:projects
ID: 40191377
Not sure what I did but I just went over all of the code, including what you gave me and it's working perfectly now.

Thank you!
0
 
LVL 85

Expert Comment

by:ozo
ID: 40191389
Maybe none of the hops timed out this time.
0
 

Author Comment

by:projects
ID: 40191395
Don't think so, there's only one which always times out and it was always hop 10.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question