Operating System - HP-UX
1826402 Members
4082 Online
109692 Solutions
New Discussion

Can someone explain this...

 
SOLVED
Go to solution
Ratzie
Super Advisor

Can someone explain this...

Can some one explain this...
IPLIST=/the/correct/path/sw_ip.txt
for IP in $(cat $IPLIST)
do
echo $IP
echo $IP
echo "$IP"
echo $IP ","
echo "$IP ,"
echo "$IP, changed passwd."
echo "$IP changed passwd."
echo $IP", changed passwd."
done

Output:
123.456.20.50
123.456.20.50
123.456.20.50
,3.456.20.50
,3.456.20.50
, changed passwd.
changed passwd.
, changed passwd.


9 REPLIES 9
Patrick Wallek
Honored Contributor

Re: Can someone explain this...

The problem with lines 3 thru 8 is that the shell doesn't know exactly where the varialbe $IP ends and the rest starts.

To get the behavior you expect use the form ${IP} and it will work much better.
Ratzie
Super Advisor

Re: Can someone explain this...

Tried that still no go...
for IP in $(cat $IPLIST)
do
echo ${IP}
echo ${IP}
echo "${IP}"
echo ${IP} ","
echo "${IP} ,"
echo "${IP}, changed passwd."
echo "${IP} changed passwd."
echo ${IP}", changed passwd."
done
123.456.20.50
123.456.20.50
123.456.20.50
,3.456.20.50
,3.456.20.50
, changed passwd.
changed passwd.
, changed passwd.
John Poff
Honored Contributor
Solution

Re: Can someone explain this...

Hi,

I cut and pasted your shell script and I tried it on one of my 11i boxes. It seems to work correctly:

>./ip_test.sh
123.456.20.50
123.456.20.50
123.456.20.50
123.456.20.50 ,
123.456.20.50 ,
123.456.20.50, changed passwd.
123.456.20.50 changed passwd.
123.456.20.50, changed passwd.

JP
Patrick Wallek
Honored Contributor

Re: Can someone explain this...

I tried this and it worked fine:

#!/usr/bin/sh

IP=123.45.67.89
echo ${IP}
echo "${IP}"
echo ${IP} ","
echo "${IP} ,"
echo "${IP}, changed passwd."
echo "${IP} changed passwd"
echo ${IP}", changed passwd"


OUTPUT:
123.45.67.89
123.45.67.89
123.45.67.89 ,
123.45.67.89 ,
123.45.67.89, changed passwd.
123.45.67.89 changed passwd
123.45.67.89, changed passwd
Sridhar Bhaskarla
Honored Contributor

Re: Can someone explain this...

I believe this is a follow-up messages to

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=426931

I have no idea why your shell is behaving like this. You should have gotten atleast from echo "$IP changed passwd." withouth using {} around the variable.

Can you try it in another window?. Add #!/usr/bin/ksh to the starting of the script and see if it makes any difference.

#!/usr/bin/ksh
IPLIST=/the/correct/path/sw_ip.txt
for IP in $(cat $IPLIST)
do
echo $IP
echo $IP
echo "$IP"
...

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Patrick Wallek
Honored Contributor

Re: Can someone explain this...

Have you checked the file you are getting the IPs from? I would check and make sure the data is what you think it is.

I just tried the following with no problem:

Script:
#!/usr/bin/sh
IPLIST=iplist
for IP in $(cat $IPLIST)
do
echo ${IP}
echo "${IP}"
echo ${IP} ","
echo "${IP} ,"
echo "${IP}, changed passwd."
echo "${IP} changed passwd"
echo ${IP}", changed passwd"
done

iplist file:
# cat iplist
12.34.56.78
90.12.34.56

OUTPUT:
12.34.56.78
12.34.56.78
12.34.56.78 ,
12.34.56.78 ,
12.34.56.78, changed passwd.
12.34.56.78 changed passwd
12.34.56.78, changed passwd
90.12.34.56
90.12.34.56
90.12.34.56 ,
90.12.34.56 ,
90.12.34.56, changed passwd.
90.12.34.56 changed passwd
90.12.34.56, changed passwd
Ratzie
Super Advisor

Re: Can someone explain this...

FOUND MY PROBLEM!!!

Had no idea that the file was created in notepad...

Had to dos2ux!
Works fine now!


THanks for all your help!
I know everyone has spent alot of time on it!
Rodney Hills
Honored Contributor

Re: Can someone explain this...

I think IP is getting set to "123.456.20.50\r".

The \r causes a carriage return thus over writing the first part of the data.

HTH

-- Rod Hills
There be dragons...
Bill Hassell
Honored Contributor

Re: Can someone explain this...

Notepad is like vi, it creates a plain ASCII file (unlike Wordpad or MS Word which has numerous binary codes). However, since Notepad runs on a PC, the file would either be transfered by ftp or through SAMBA/CIFS. In either case, the foreign operating system imposes it's own file layout, something that makes file sharing less than convenient. Although not as common, sharing files with Macs and mainframes would have the same issues. Two useful commands for inspecting shared files:

cat -v some_file
xd -xc some_file


Bill Hassell, sysadmin