As a web applications developer I’m often doing web development work with static HTML pages. Shell scripts can be very powerful when it comes to web development I have some techniques that I use that save me time on tasks.
I recently used a series of shell scripts for web development to finish off a site that was split into two sections a content section and a template. The template was designed and finished but the content pages was not wrapped within the HTML pages. I did not want to use a dynamic scripting language like PHP to generate the pages so I came up with a few scripts to make my life a little easier and save me from copying and pasting in all of the content into the correct pages.
The first thing was that the person who designed the site interchanged between .html and .htm within the pages so what I did to fix that and make future work less complicated I renamed all of the files with a small shell script:
for x in $(ls -1|grep html);
RENAME=$(echo $x | cut -d . -f -1)
mv $x $RENAME.htm
That may not be the most elegant way to do it, but it took care of the file renaming for me. All I had to do now was make sure all of the pages did not link to the old HTML pages. To do this I used sed and a loop to find and replace as followed
for x in $(ls -1|grep htm);
cat $x | sed 's/html/htm/g' > $x
Since I was just adding content to the main section of the template I divided the HTML into two sections, the HTML that would go above the content and the HTML that would go below. I split the HTML into two separate files a file named “top.txt” and a file named “bottom.txt”. Since the content was designed in a uniform matter I simply appended the “bottom.txt” file by redirecting output:
cat bottom.txt >> contentfile1.htm
Now that the files had the bottom section of the template the top.txt had to be prefixed. To achieve this I used a file reversing method. Basically I overwrote the top file with the reversed version by using tac
tac top.txt > new_top; mv new_top top.txt
This is one way to reverse and overwrite a file (the other is in the next step).
Once I reversed the file, I performed the following:
cat contentfile1.htm | tac > contentfile1.htm; cat top.txt >> contentfile1.htm; cat contentfile1.htm | tac > contentfile1.htm
This line is quite long but what its doing is simple. Firstly it reverses the file, then it appends the already reversed top.txt. The last step is to reverse the whole thing again.
Sometimes depending on how many files I have to process I will put this in a simple loop but if its a reasonable amount of files then I’ll do it by hand.
One thing to always remember when using shell scripts on web development work is to create backups. There is nothing worse than saving time by using a script to do the work but having to write more scripts to undo things that went wrong. In this case I a lot of time but not as much as I could have if I had kept backups for each step of the way.