LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SED - minor changes work - Larger doesn't (working and non working code included) (https://www.linuxquestions.org/questions/programming-9/sed-minor-changes-work-larger-doesnt-working-and-non-working-code-included-586394/)

Nimoy 09-22-2007 03:13 PM

Weird....
 
Quote:

Originally Posted by jozyba (Post 2900394)
Bl**dy Ubuntu! :)

On Ubuntu 'sh' is symlinked to 'dash', so when you call the script by going 'sh myscript.sh' it will ignore the '#!/bin/bash' at the head of the script and use dash instead. dash cannot cope with the syntax in line 49 of your script.

The solution is either to run it with 'bash myscript.sh' or just to use 'chmod u+x myscript.sh' to make it executable, then call it with './myscript.sh'.

Tried both solutions - No error messages this time. So I presume the syntax is ok as such.

However no changes inside any of the HTML files - Got a .bak file for every file.

Maybe because the script copies the HTML files and doesn't perform the change in every html file ?

jozyba 09-22-2007 03:27 PM

Quote:

Originally Posted by Nimoy (Post 2900408)
Tried both solutions - No error messages this time. So I presume the syntax is ok as such.

Well, that's progress.

Quote:

However no changes inside any of the HTML files
The script will only make changes if the *precise* string in the "$substring" variable is found. If no changes are being made, then that substring is not being found. So now it's time for you to check that the substring in your script is correct. If your search clue contains even an extra space or newline character it will not match.

Nimoy 09-22-2007 04:34 PM

Working!!!
 
Changed the substring I wanted to replace - so the script now looks like this:


#! /bin/bash

substring='<a HREF="index.html" TARGET="_top">Home</a> '
replacement='<script type="text/javascript"><!--
google_ad_client = "pub-5045815486985038";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
//2007-08-14: globabilityaug2007setup
google_ad_channel = "5631073777";
google_color_border = "000000";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br><br>
<a HREF="index.html" TARGET="_top">Home</a>'

for file in *.html; do
cp $file $file.bak # alternatively use 'mv'
file_contents="$(<$file.bak)"
echo "${file_contents//$substring/$replacement}" >$file
done

The above was saved into a text file called winner.sh and I have set the perms as you explained earlier in the thread so the script is executable by calling ./winner.sh

HOURS UPON HOURS OF DREARY SYNTAX REPLACEMENT HAVE NOW BEEN SHAVED AWAY FROM MY TIME SPENT MAKING THESE CHANGES - AS WELL AS FUTURE ONES!

THANKS A MILLION!!!!!!!!!!!!!


All times are GMT -5. The time now is 10:26 PM.