Advertisement

How do I make this shell script

Started by October 20, 2003 07:51 AM
10 comments, last by clum 20 years, 10 months ago
I am making a web site. Every page is going to have the same logo on top, navigation bar on the side, background colour, etc. Only the main table cell will be different. Obviously, the solution is to make a template. The only thing is, what if I change the template? I''d have to manually change every single page on the site. So this is the solution I thought of. I''d have a directory (let''s say called fillins) with all the htmls of the web site, just without a navigation bar and all that. Then I''d have a directory, let''s say combined, which should have the pages found in fillins, combined with the template. And these two directories would be together in a directory which also has the template and a shell script which combines the template and any given html file and puts it in combined. The problem is, I''ve never made anything more than a really simple shell script before and I tried, but couldn''t get this to work. I pose this shell script as a challenge (that''s the best way to get someone to write it for me). I use bash, but any standard shell (i.e. sh, csh, ksh, bash, tsh, etc.) is fine with me. Of course, take into account that I want the title of the fillin html page to remain. Therefore, I would want the combined file to be: 1) The first line of the fillin file 2) The contents of the file called first.html 3) The fillin file from the third line until the second last line 4) The contents of the file called last.html The second line of the fillin file would be ignored (it would say something like , while first.html might add something to the head, add an attribute to body, and but in a navbar). The last line would also be ignored (it would say </body></html>, but last.html would close any open tags from first.html like tables first). Here''s a sort of pseudo-code version to show exactly what I mean: For each command line argument: head -1 fillin/$curarg > combined/$curarg cat first.html >> combine/$curarg tail fillin/$curarg -remove first 3 lines | head -remove last line >>combine/$curarg cat last.html >> combine/$curarg Then I would run it with something like ./combine fillin/*.html or even a Makefile. Can anyone write such a script for me or at least point me to a really good tutorial on how to write scripts. While we''re talking about this anyway, the server where this webpage will be held also supports php and cgi. I have written cgi scripts in C++. I would be doing anything too complicated on my website, but is it worth it to learn php or perl anyway?
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
To give a very simple situation in which the shell script would work:

the file structure:
website/|.....combine (a shell script)|.....first.html|.....last.html|.....fillin/      |.....index.html|.....combined/      |.....index.html

first.html might be:
</head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0><table width="100%" cellpadding=0 cellspacing=0 border=0>	<tr><td align="center">		<table width="95%" cellpadding=0 cellspacing=0 border=0>			<tr valign="top"><td valign="middle"><center>Header</center>			</td></tr>		</table>	</td></tr>	<tr><td align="left">		<table width="100%" cellpadding=0 cellspacing=0 border=0>			<tr>				<td width="20%" align="left"><center>Navigation</center>				</td>				<td width="80%">

last.html:
				</td>			</tr>		</table>	</td></tr></table></body></html>

and /fillin/index.html:
<html><head><title>Main page</title></head><body><h1>Welcome to this website</h1>Welcome. Bye.</body></html>

The file that I don''t have the source for is combine, the shell script.
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
Advertisement
Never mind all that - I''ve just discovered that my server runs Apache and supports SSI (server-side includes) which removes any need for my script. I still wouldn''t mind learning how to write shell scripts well, though.
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
Off the top of my head (i.e. without testing):

#!/bin/bashfor file in $(ls fillins)do  head -1 fillins/$file > combined/$file  cat first.html >> combined/$file  cat fillins/$file | tail +3 | head -n $[ $(wc -l fillins/$file | gawk ''{print $1;}'') - 1 ]  cat last.html >> combined/$filedone 


Okay, it wasn''t entirely off the top of my head. I had to look up some man pages on the web.
---New infokeeps brain running;must gas up!
quote: Original post by clum
Never mind all that - I''ve just discovered that my server runs Apache and supports SSI (server-side includes) which removes any need for my script. I still wouldn''t mind learning how to write shell scripts well, though.


Then your server probably also supports php. Learning a little php (it''s similiar to perl and C, somewhere between the two), can go a long way in making a website. It''s good stuff.
quote: Original post by Flarelocke
Off the top of my head (i.e. without testing):

#!/bin/bashfor file in $(ls fillins)do  head -1 fillins/$file > combined/$file  cat first.html >> combined/$file  cat fillins/$file | tail +3 | head -n $[ $(wc -l fillins/$file | gawk ''{print $1;}'') - 1 ]  cat last.html >> combined/$filedone  


Okay, it wasn''t entirely off the top of my head. I had to look up some man pages on the web.

Thank you very much. Even though I have SSI, it will be fun to try out.
AP - Thanks for your advice. I actually mentioned the php support in the OP, but I suppose it was a bit long for most people to read the whole thing.
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
Advertisement
quote: Original post by clum
Never mind all that - I''ve just discovered that my server runs Apache and supports SSI (server-side includes)
Good choice
quote: I still wouldn''t mind learning how to write shell scripts well, though.
BASH Programming - Introduction HOW-TO, but why bother with shell scripts? Perl is so much more powerful for anything more advanced than running a few commands in sequence.
ahem.

cat first.html file last.html > done
quote: cat first.html file last.html > done
Doesn''t fit the requirements.

Though I wonder if "echo $(head -1 file) | cat first.html file last.html | uniq" wouldn''t work. Uniq removes duplicate consecutive lines.
---New infokeeps brain running;must gas up!
quote: Original post by CWizard
but why bother with shell scripts? Perl is so much more powerful for anything more advanced than running a few commands in sequence.


Then again, Python and Ruby are just as good as perl, just a powerfull if not more( in the case of Ruby that is ). And there are times when writing a small shell script can be usefull. Especially on old version of Unix where you didn''t have perl installed. Say like the SunOS box at my old work. The admin didn''t install perl on it as it was only supposed to be a mail server. When I had to code for it, the closest thing to a scripting language I had was shell. Nothing else. So it is still usefull.

[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com][ Administrator & WebMaster GuLSE]
[Cyberdrek | ]

This topic is closed to new replies.

Advertisement