Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird bash shell script side-effect

Status
Not open for further replies.

kharybdis

Technical User
Mar 3, 2001
9
US
I'm using a basic bash script to generate an HTML page. It's just using echo, since only one thing needs to change. That one thing is a variable $extip, which = my current IP.
I've used this technique a bunch of times and it's worked just fine, but now I have a strange problem. Here's the pertinent script:

#!/bin/bash
#
extip="`/sbin/ifconfig eth1 | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'`"
echo &quot;<HTML>&quot;
echo &quot;<HEAD>&quot;
echo &quot;<TITLE>n8shome</TITLE>&quot;
echo '<SCRIPT LANGUAGE=&quot;JavaScript&quot;><-- setTimeout (&quot;changePage()&quot;, 3000); function changePage() { if (self.parent.frames.length != 0) self.parent.location=&quot;http://'$extip'&quot;; }//--></SCRIPT>'
echo &quot;</HEAD>&quot;

Now when I run this shell script, the HTML comes out just fine except for one thing. Notice the JavaScript line in the code above, specifically the $extip variable. Now here's how that same line comes out:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
setTimeout (&quot;changePage()&quot;, 0)
function changePage() {
if (self.parent.frames.length != 0)
self.parent.location=&quot;http://xxx.xxx.xxx.xxx^M/cam/index.html&quot;;
}
//-->
</SCRIPT>

For some reason, suddenly I have an extra ^M behind the IP. It screws up the function of the script itself. I'm stumped. I tried to keep everything simple so as to avoid strange errors, but that's exactly what I got. Although I can appreciate true irony, I'd much rather see it in a Greek tragedy than in my code.

Details:
Mandrake 7.2
Bash shell
x86 platform
AMD K6/380

Again, I'm currently using the exact same technique for importing my current IP in about 6 different scripts, four of which are generating HTML the same way. It only goofs up when I try to echo it out while inside that JavaScript line. *shrug*

Thanks in advance, even if you can't make a suggestion I appreciate you reading this long message.

nathan
 
Usually the ^M denotes a line break or carriage return. It's unusual to see this in Unix, though, because usually I see it when I open a Windows-edited text file in Unix.
As I understand it, Windows adds a newline &quot;\n&quot;, and a carriage return &quot;\r&quot;, while Unix tends to just generate a newline when the enter key is pressed in a text file. For some reason the carriage return is interpreted as ^M in Unix.

I'm not sure exactly what this has to do with your script, but maybe it puts you on the right track.

Also, rather than use

Code:
echo '<SCRIPT LANGUAGE=&quot;JavaScript&quot;><-- setTimeout (&quot;changePage()&quot;, 3000); function changePage() { if (self.parent.frames.length != 0) self.parent.location=&quot;[URL unfurl="true"]http://'$extip'&quot;;[/URL] }//--></SCRIPT>'

Try escaping your quotes inside the string and setting your variable on its own line.

Code:
echo &quot;<SCRIPT LANGUAGE=\&quot;JavaScript\&quot;><-- setTimeout (\&quot;changePage()\&quot;, 3000); function changePage() { if (self.parent.frames.length != 0) self.parent.location=\&quot;[URL unfurl="true"]http://&quot;[/URL]

echo $extip

echo &quot;\&quot;; }//--></SCRIPT>&quot;

And if this doesn't work, then you just need to trim the last character off $extip before you echo it.
 
Thanks for the reply rcyamor!
Your suggestion worked and it didn't work.
Before, I got the ^M if I ran:
bash home.sh
and
bash home.sh > index.html

Now, it doesn't happen on:
bash home.sh

So, for some reason, redirecting it causes the extra characters. I'm going to experiment a little.
I'll keep you updated.

Thanks again!

 
Ok, here's where I'm at. I couldn't get around the redirect problem, so I thought I'd 'sed' the ^M out with this . . .

sed &quot;s/^M//g&quot; <index.html > index.new

but it doesn't seem to be doing anything. Is sed confusing the '^M' for an actual carriage return?

I've tried `^M`, '^M', and \^M, but none seem to work.

Still experimenting . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top