Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (2) </ [1] 2 >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: CGI scripting with Bash, first toy....< Next Oldest | Next Newest >
mikshaw Offline

Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 04 2007,20:16 QUOTE

Started messing around with cgi on my LAN website...realized I still don't want to attempt to learn Perl or PHP so I decided to try some stuff with Bash.

I hear that Bash isn't the most secure thing to use for server side programming, but since this is a private server I thought it would be worth some experiments.

This first one is a tool to display high resolution ascii art on a web page.
I used to write individual web pages from a template so I wouldn't need to manually reduce the font size to see the image in full. That was annoying.
This script does that for me now, on-the-fly.

Newer Changes:
Added an optional external css file
No longer makes link to *.txt if no files are found
Some code cleanup
Displays filename in "not found" message
Displays directory name above links
FILENAME and REALFILE variables separated

New Changes:
Script now contains its own <pre> style, so an external style sheet is not needed.
Removed the unnecessary "this is what might be wrong" text.
Made font size and charset easily customizable.
Removed some other useless bytes here and there.

Code Sample
# 2007 mikshaw
# cgi script to display high resolution ascii text files in a web page.
# Normally a text file containing a high-res image would extend far beyond
# the limits of a browser or text editor window, making it impossible to
# appreciate the image without manually setting your font to a tiny size.
# This script will display a text file condensed into a visually appealing
# space. A fairly high resolution ascii image should display nicely within
# the space of a maximized 1024x768 browser window. My tests with files of
# 500-character lines went slightly off the right side of the visible page
# using a 3pt font on a 1024x768 screen.
# Additionally, the script will search for *.txt files in the specified
# directory and build a list of links to them at the bottom of the page.
# I'm very new to cgi programing, so i welcome any suggestions or warnings.
# The script is summoned like this:
# filename is the filename without a path. If there is no "dir",
# it will be assumed that the file is in the root of $MYSITE
# dirname is an optional subdirectory name relative to $MYSITE.
# If there is no QUERY_STRING at all, it will just build links
# from text files in the root of the $MYSITE directory.
# If you use an additional style sheet, take note that visual elements
# in this script are <a>, <h3>, <p> and <pre class="ascii">

# check for and split parameters
if [ -n "$QUERY_STRING" ]; then
NEWSTRING=`echo $QUERY_STRING | sed 's/&/ /g'`
for i in $NEWSTRING; do
varname=`echo $i | cut -d= -f1`
varvalue=`echo $i | cut -d= -f2`
# set bash variables from params
case $varname in
dir) ASCIIDIR="$varvalue";;
file) FILENAME="$varvalue";;

### CONFIGURATION ########################
# the absolute path to your ascii files
# relative or absolute URL to additional style sheet (optional)
# font size and line height of ascii art
# character set

cat << TOP
<html><head><meta http-equiv="Content-Type" content="text/html; charset=$CHARSET">
<STYLE type="text/css">pre.ascii { font-size: $FNSZ; padding: 0; line-height: $LNSZ; }</STYLE>
[ -n "$STYLE" ] && echo "<link href=\"$STYLE\" rel=\"stylesheet\" type=\"text/css\">"
echo "</head><body><h3>$FILENAME</h3>"
if [ -f "$REALFILE" ]; then
echo "<pre class=\"ascii\">"
echo "</pre>"
echo "<p>"File \"$FILENAME\" not found"</p>"
echo "<p>Text files available in this directory ($ASCIIDIR):<br>"
for i in $MYSITE/$ASCIIDIR/*.txt
NEWFILE=`basename "$i"`
[ -f "$i" ] && echo "[<a href=\"/cgi-bin/$ASCIIDIR&file=$NEWFILE\">$NEWFILE</a>] "
echo "</p>"
cat << END
<p><a href="/">&lt;&lt; BACK HOME</a></p>

Back to top
Profile PM WEB 
^thehatsrule^ Offline

Group: Members
Posts: 3275
Joined: July 2006
Posted: Jan. 04 2007,21:23 QUOTE

heh, although I personally prefer PHP (you'll pick it up very quick as the syntax is very flexible), the use of bash+cgi is interesting.

One misc html comment though:
Wouldn't using
be better (esp. if you plan on using css?)
Back to top
Profile PM 
mikshaw Offline

Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 04 2007,21:40 QUOTE

The </li> closing tag is optional, acording to the html documents I've read.  I have no idea how the closing tag might affect css, though, as you mentioned...I've never styled lists specifically. The </br> tags were already in there before i thought it would look better as a list, and I simply forgot to remove them. They're now removed from my local copy, thanks. I'll probably put </li> tags in just to be thorough.

I'll eventually save one of the generated pages and check the syntax against strict html, but for now I'm thinking more about its functionality than anything else...I want to try some other ways to make it less site-specific, and basically just fool around with it until i get bored and look for a new project.

Back to top
Profile PM WEB 
mikshaw Offline

Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 05 2007,23:36 QUOTE

I'm working on (or planning, anyway) a stripped down version of this script that will use a URL as a query string and load that instead of needing a local file. I think this would be more portable, considering many website owners probably don't run their servers on their own machines. It makes it difficult to use a full directory path if you don't know what the path is.
Maybe it isn't all that difficult, though...i'm sure if you need the path, and have permission to run scripts on your server, you could probably at least get the path to cgi-bin and work from there...

Anyway, I have no idea how to use the contents of a URL as part of an html document, so it might not happen =op

Back to top
Profile PM WEB 
mikshaw Offline

Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 10 2007,20:16 QUOTE

Here's another one....very simple and needs some work to make it more flexible and look better.
This script displays a random image from a directory and a button to load a new random image.
Code Sample

cat << BEGIN
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Random Image</title>
<form action="/cgi-bin/">
<input type=submit value="random image">

# url to directory
# full path to same directory

# this needs some work. it assumes all files are images
num_pics=`ls -A "$PIC_DIR/" | grep -c .`
list_pics=`find "$PIC_DIR/"`
echo "<img src=\"$PIC_URL/`basename $rand_image`\"></body></html>"

Back to top
Profile PM WEB 
6 replies since Jan. 04 2007,20:16 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (2) </ [1] 2 >/
reply to topic new topic new poll
Quick Reply: CGI scripting with Bash

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code