Code Sample |
#!/bin/bash # 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: # #http://www.my.host/cgi-bin/showascii.sh?dir=dirname&file=filename # # 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'` varnum=1 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";; esac varnum=$((varnum+1)) done fi ### CONFIGURATION ######################## # # the absolute path to your ascii files MYSITE=/opt/lighttpd/lansite/www/ascii_art # relative or absolute URL to additional style sheet (optional) STYLE= # font size and line height of ascii art FNSZ=3pt LNSZ=2.25pt # character set CHARSET=ISO-8859-1 # ########################################## cat << TOP <html><head><meta http-equiv="Content-Type" content="text/html; charset=$CHARSET"> <title>$FILENAME</title> <STYLE type="text/css">pre.ascii { font-size: $FNSZ; padding: 0; line-height: $LNSZ; }</STYLE> TOP [ -n "$STYLE" ] && echo "<link href=\"$STYLE\" rel=\"stylesheet\" type=\"text/css\">" echo "</head><body><h3>$FILENAME</h3>" [ -n "$ASCIIDIR" ] && REALFILE="$MYSITE/$ASCIIDIR/$FILENAME" || REALFILE="$MYSITE/$FILENAME" if [ -f "$REALFILE" ]; then echo "<pre class=\"ascii\">" cat "$REALFILE" echo "</pre>" else echo "<p>"File \"$FILENAME\" not found"</p>" fi echo "<p>Text files available in this directory ($ASCIIDIR):<br>" for i in $MYSITE/$ASCIIDIR/*.txt do NEWFILE=`basename "$i"` [ -f "$i" ] && echo "[<a href=\"/cgi-bin/showascii.sh?dir=$ASCIIDIR&file=$NEWFILE\">$NEWFILE</a>] " done echo "</p>" cat << END <p><a href="/"><< BACK HOME</a></p> </body></html> END |
Code Sample |
cat << BEGIN <html><head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Random Image</title> </head><body> <form action="/cgi-bin/rand_image.sh"> <input type=submit value="random image"> </form> BEGIN # url to directory PIC_URL='/rand_images' # full path to same directory PIC_DIR='/opt/lighttpd/lansite/www/rand_images' # this needs some work. it assumes all files are images num_pics=`ls -A "$PIC_DIR/" | grep -c .` list_pics=`find "$PIC_DIR/"` pic_array=($list_pics) rand_image="${pic_array[$((RANDOM%num_pics))]}" echo "<img src=\"$PIC_URL/`basename $rand_image`\"></body></html>" |