bash variable limitation?

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
projectsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What is in $LIST?  Does it contain any * characters?
0
projectsAuthor Commented:
It's the results but listed over and over and over again with the count changing positions here and there.
0
ozoCommented:
Did $( traceroute -n xxx.com | awk '{if(NR>1)print $2}') produce any "*" characters?

If so, try
set -f
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

projectsAuthor Commented:
# 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
ozoCommented:
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
projectsAuthor Commented:
Can you expand on that please, I'm not sure I'm understanding that.
0
ozoCommented:
#!/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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
projectsAuthor Commented:
Inside the script, still only shows 7 hops.
0
ozoCommented:
Since your script originally showed 25, that sounds like an improvement.
0
projectsAuthor Commented:
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
ozoCommented:
Maybe none of the hops timed out this time.
0
projectsAuthor Commented:
Don't think so, there's only one which always times out and it was always hop 10.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.