Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > [req] Command to add line to text file (from shellscript)

[req] Command to add line to text file (from shellscript)
Thread Tools
Dedicated MacNNer
Join Date: Nov 2003
Location: Oslo, Norway
Status: Offline
Reply With Quote
Sep 23, 2005, 12:35 PM
 
I need help of a shellscript/bash "guru" on this case...

I build and share G4 optimized Camino builds. The build process is fully handled by a shellscript, but I need to manually edit my index.html (add link to my latest build). So I seek information on how to automate this part as well?

The lines in my index.html looks like this:
Code:
<a href="Trunk/Camino-ppc7450-2005.09.21.dmg">Camino-ppc7450-2005.09.21.dmg</a><br> <a href="Trunk/Camino-ppc7450-2005.09.22.dmg">Camino-ppc7450-2005.09.22.dmg</a><br> <a href="Trunk/mozconfig.txt">.mozconfig</a>
I want to add a line below the other ones with the current date. Like:
<a href="Trunk/Camino-ppc7450-2005.09.21.dmg">Camino-ppc7450-2005.09.23.dmg</a><br>
where the date is generated from "date +%Y.%m.%d"


I really dont know where to start (have searched Google, but not found anything usefull)...
Thanks in advance!
     
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Sep 23, 2005, 09:27 PM
 
Code:
#!/bin/sh NEW_BUILD_LINE=`date +'<a href="Trunk/Camino-ppc7450-%Y.%m.%d.dmg">Camino-ppc7450-%Y.%m.%d.dmg</a><br>'` sed -e "/mozconfig/i\\ $NEW_BUILD_LINE" your_file.html > new_file.html mv new_file.html your_file.html
Here's how it works: (if you're already writing shell scripts, then you probably know some of this already)

The first line creates the line we will insert in the file. As you're probably aware, you can tell 'date' to format its output any way you like; I figured making the whole line at once would be easiest.

'sed' is short for stream editor--one of several commands to edit files from the command line ('awk' is another such command). The -e means execute the following command. Sed will print out all lines from the file verbatim by default. "/mozconfig/" says to do something with lines that contain mozconfig. The next bit 'i\\' says (for these matching lines) to insert the following text. Actually, sed expects 'i\', not 'i\\'; we put two backslashes because one is stripped off by the shell script. The next line is the text we insert, and that's the end of the sed command. The file we take as input is "your_file.html".

Sed writes the output to standard out, so we can redirect its output to new_file.html, which we then move back over the original. I recommend not putting this line in until you are sure everything else is working correctly!
     
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Sep 23, 2005, 11:26 PM
 
Why don't you make index.html into a php file? Instead of a search and replace you could just insert a value with a variable or file include. Much easier to do.

add this to index.html:

<? include ('build_date.txt'); ?>

replace your link with this:
<a href="Trunk/Camino-ppc7450-<? echo $build_date; ?>.dmg">Camino-ppc7450-<? echo $build_date; ?>.dmg</a><br>


now your script just creates a simple text file called build_date.txt in the same folder as index.html.

the file contains just this:

<? $build_date = '2005.09.22'; ?>


so your script just needs to overwrite this file with something like this:

echo "<? $build_date = '`date +%Y.%m.%d`"'; ?>" > build_date.txt

(it's been a while so don't hold me to the shell script syntax)

sed will do the trick, but this may be a lot easier then debugging a complex regex

you'll need to make sure apache is set up to serve php files either using index.php or having php handle all html files

It's cool that there is always another way to do it.
You can take the dude out of So Cal, but you can't take the dude outta the dude, dude!
     
albook  (op)
Dedicated MacNNer
Join Date: Nov 2003
Location: Oslo, Norway
Status: Offline
Reply With Quote
Sep 24, 2005, 01:37 AM
 
Juggle5. Thanks a lot!
Those lines does exactly what I want. Now, as soon as I modify my shellscript, I have a fully automated Camino build/upload system...

Gavin. My host (Apple iDisk) dont have PHP support, so I have to use basic html.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:11 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2