Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 20 Sep 2019, 01:14
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
CGI scripts with busybox httpd
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [21 Posts]   Goto page: 1, 2 Next
Author Message
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Sun 27 Jan 2019, 06:48    Post subject:  CGI scripts with busybox httpd
Subject description: .. or "creating web based GUIs for your programs"
 

I've been playing with shell based CGI scripts..

These essentially allow you to use shell script and all your normal system
commands inside a web page to create dynamic web pages, or in other
words, web based GUIs for system/terminal programs.

I wanted a lightweight way of creating dynamic web applications without installing any extra programs.

This method uses only the 'httpd' busybox applet, and BASH.

To use these examples, you'll need to create the folder ~/bin/ and add it to your $PATH:

Code:
mkdir /root/bin
export PATH=/root/bin:$PATH


SETUP:

1. Create the folders /root/www/cgi-bin

2. Save the scripts below as /root/www/cgi-bin/test.sh and ~/www/cgi-bin/upload.sh

3. Run `chmod 755 /root/www/cgi-bin/test.sh` to make it executable and give it the right permissions

4. Run `DOCUMENT_ROOT=/root/www httpd -h /root/www -p 127.0.0.1:8080 -u nobody:nobody`

(Or you could start your web server with this wrapper I made, to get extra security
and automatic folder creation, file ownership/permission handling, etc)

5. Visit http://localhost:8080/ in your browser.

FEATURES OF TEST SCRIPT:

You can add ?foo=bar&stuff=whatever to the URL and these values will be displayed on the page as $GET['foo'], etc ..

You can submit the form on the page and then the form field values will be displayed on the page as $POST['foo'], etc ..

You can upload files - BUT BINARY FILES GET BROKEN IN THE UPLOAD ... Any fixes for that?!

File: test.sh
Code:
#!/bin/bash

#===============================================================================
# Description:
#
#   a test script, demonstrating how to parse GET and POST
#   variables into associative arrays with BASH, httpd and CGI.
#
# Installation:
#
#   chmod 755 this script, and put it in your CGI bin (somewhere
#   like '$HOME/www/cgi-bin/test.sh')
#
# Usage:
#
#   Run the command `httpd -p 127.0.0.1:8080 -h ~/www` and visit
#   the URL 'http://localhost/cgi-bin/test.sh' in a web browser.
#
#   Adding a query string to the URL, or submitting the form on
#   the page will create $POST and $GET arrays containing the
#   key/value pairs of the POST/GET data.
#
#
# More info:
#
#   https://stackoverflow.com/questions/3919755/how-to-parse-query-string-from-a-bash-cgi-script
#
#===============================================================================

# Set some vars for later

title="CGI demo"

#===============================================================================

# Parsing GET and POST variables is horrible, let's make it easier.
# Let's put the query string and POST variables in into nice arrays.

# get POST vars (if any), with support for multi-line vars
POST_STRING="$(cat)"

# handling file uploads


if [ "$REQUEST_METHOD" = "POST" ]; then

  if [ "$(echo "$CONTENT_TYPE" | grep -m1 'multipart/form-data;')" != "" ];then

    if [ "$CONTENT_LENGTH" -gt 0 ]; then
      boundary=$(echo -n "$POST_STRING" | head -1 | tr -d '\r\n');
      filename=$(echo -n "$POST_STRING" | grep --text --max-count=1 -oP "(?<=filename=\")[^\"]*");
      file_content="$(echo -n "$POST_STRING" \
        | sed '1,/Content-Type:/d' \
        | tail -c +3 \
        | head --lines=-1 \
        | head --bytes=-4 \
        | head -n -4)"
    fi

  else

    # parse $QUERY_STRING
    saveIFS=$IFS                             # save IFS (internal field separator)
    IFS='=&'                                    # use '=' and '&' as internal field separators
    get_array=($QUERY_STRING)      # create an array from $QUERY_STRING
    post_array=($POST_STRING)       # create an array from $POST_STRING
    IFS=$saveIFS                             # restore IFS to its original state

    # Let's create associative array, so we can access the
    # query string and POST values like so: $GET['foo'] and $POST['foo']

    # The keys will be accessed using an exclamation point ${!array[@]},
    # the values are accessed using ${array[@]}, the total number of items
    # in the array can be accessed using a hash - ${#array[@]}.

    # Declare, is used to set variables, array  and attributes.The -a option
    # creates indexed arrays, -A creates associative arrays.

    # declare the $GET and $POST associative arrays (still empty at this point)
    declare -A GET
    declare -A POST

    # add the key/value pairs to $GET
    for ((i=0; i<${#get_array[@]}; i+=2))
    do
        key=${get_array[i]}
        value=${get_array[i+1]}
        GET[$key]=$value
    done

    # add the key/value pairs to $POST
    for ((i=0; i<${#post_array[@]}; i+=2))
    do
        key=${post_array[i]}
        value=${post_array[i+1]}
        POST[$key]=$value
    done

  fi

fi
#===============================================================================

# BEGIN OUTPUT

# IMPORTANT - set the content type first, then an empty newline!

echo 'Content-type: text/html; charset=UTF-8'
echo 'Status: 200 OK'
echo ''
echo ''

#===============================================================================

# BEGIN PAGE HTML
echo '<!DOCTYPE html>'
echo '<html>'
echo "<head><title>$title</title></head>"
echo "<body>"
echo "<h1>$title</h1>"

echo "<br>========================================================<br>"

echo "<p>ENV VARS</p>"
echo '<pre>'
uname -a
echo -n 'user = '; whoami
env | sort -u
echo "</pre>"

echo "<br>========================================================<br>"

# if we have a query string, print the relevant vars
if [ "$QUERY_STRING" != '' ];then
  echo '<p>$QUERY_STRING:</p>'
  echo "<pre>"
  echo $QUERY_STRING
  echo "</pre>"

  echo '<p>$GET[@] array:</p>'
  echo "<pre>"
  # print the key/value pairs:
  for x in "${!GET[@]}"
  do
    echo '$GET'"[$x] = ${GET[$x]}"
  done
  echo "</pre>"
else
  echo "<p>Add a query string to the URL to see it parsed here.</p>"
fi

echo '<br>========================================================<br>'

# if we have a POST vars, print the relevant vars
if [ "$POST_STRING" != "" ];then
  # file uploads
  if [ "$(echo "$CONTENT_TYPE" | grep -m1 'multipart/form-data;')" != "" ];then
    echo "<br>"
    echo "<p>File upload(s):</p>"
    echo "<pre>"
    echo "boundary: $boundary"
    echo "filename: $filename"
    echo "file contents:"
    echo "</pre>"
    echo "<div style=\"background-color:#dfdfdf;\"><pre>${file_content}</pre></div><p>more text..</p>"
  else
    echo '<p>$POST_STRING:</p>'
    echo "<pre>"
    echo $POST_STRING
    echo "</pre>"

    echo '<p>$POST[@] array:</p>'
    echo "<pre>"
    # print the key/value pairs:
    for x in "${!POST[@]}"
    do
      echo '$POST'"[$x] = ${POST[$x]}"
    done
    echo "</pre>"
  fi

else
  echo '<form id="file-form" action="upload.sh" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
    </form>'

  echo '<br>========================================================<br>'

  echo '<p>Enter a month and year then click "Submit" to see the POST variables here.</p>'
  echo '<form id="text-form" method="post" action="">'
  echo '  <p>Month: <input type="text" name="month" value="" style="width: 90px" /></p>'
  echo '  <p>Year: <input type="text" name="year" value="" style="width: 90px" /></p>'
  echo '  <input type="submit" value="Submit" />'
  echo '</form>'
fi

echo "</body>"
echo "</html>"
echo ""



And upload.sh
Code:
#!/bin/bash

#======================================
# Description:
#
#   A dumb script for handling file uploads.. CANNOT handle binary files!
#
# Installation:
#
#   chmod 755 this script, and put it in your CGI bin
#
# Usage: POST the file to this script using something like this:
#
#   <form action="" method="post" enctype="multipart/form-data">
#     <input type="file" name="fileToUpload" id="fileToUpload">
#     <input type="submit" value="Upload File" name="submit">
#   </form>
#
# Or, use curl to upload files via the terminal:
#
#   curl http://localhost:8080/cgi-bin/upload.sh -F "MyFile=@/path/to/a/file"
#
#======================================

# This scripts needs the following env vars:

# DOCUMENT_ROOT          # the web server root folder (usually /var/www/)
# REQUEST_METHOD        # GET or POST
# CONTENT_TYPE              # 'multipart/form-data' or 'application/x-www-form-urlencoded'
# CONTENT_LENGTH         # length of POST data

function error500 {
  echo 'Content-type: text/html; charset=UTF-8'
  echo 'Status: 500 Internal Server Error'
  echo ''
  echo ''
}

if [ ! -d "${DOCUMENT_ROOT}" ] || \
   [ "$REQUEST_METHOD" != "POST" ] || \
   [ "$(echo "$CONTENT_TYPE" | grep -m1 'multipart/form-data;')" = "" ] || \
   [ "$CONTENT_LENGTH" -eq 0 ]; then
  error500
  exit 1
fi

#if [ ! -d ${DOCUMENT_ROOT}/downloads/ ]then
  mkdir ${DOCUMENT_ROOT}/downloads/ &>/dev/null
  chmod 744 ${DOCUMENT_ROOT}/downloads/ &>/dev/null
  chown nobody:nobody ${DOCUMENT_ROOT}/downloads/ &>/dev/null
#fi

# all POST vars are received as std input
POST_DATA="$(cat)"

# get the file meta info and contents
boundary=$(echo -n "$POST_DATA" | head -1 | tr -d '\r\n');
filename=$(echo "$POST_DATA" | sed -n '2!d;s/\(.*filename=\"\)\(.*\)\".*$/\2/;p')
file=$(echo "$POST_DATA" | sed -n "1,/$boundary/p" | sed '1,4d;$d')

# save the file
echo "$file" > ${DOCUMENT_ROOT}/downloads/"$filename" || \
{
  error500
  exit 1
}

chmod 755 ${DOCUMENT_ROOT}/downloads/"$filename"

#--------------------------

# BEGIN OUTPUT
echo 'Content-type: text/html; charset=UTF-8'
echo 'Status: 200 OK'
echo ''
echo ''

echo "File '$filename' uploaded OK"

exit 0

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK

Last edited by sc0ttman on Wed 30 Jan 2019, 18:41; edited 12 times in total
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Sun 27 Jan 2019, 08:28    Post subject:  

To run the webserver with better security, you can hide most of your environment
variables by prefixing the httpd command with an empty environment.

Like so:

Code:
env - httpd -p 127.0.0.1:8080


Or, to include only $PATH in the exported environment:

Code:
env - PATH="$PATH" httpd -p 127.0.0.1:8080


For all the info on httpd settings and using /etc/httpd.conf, see https://git.busybox.net/busybox/tree/networking/httpd.c

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK

Last edited by sc0ttman on Sun 27 Jan 2019, 19:25; edited 1 time in total
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Sun 27 Jan 2019, 11:05    Post subject:  

Related scripts and functions:

URL encode and decode BASH functions:
Code:

# https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding
urlencode() {
    # urlencode <string>
    local LC_CTYPE=C  # fix multibyte characters (thanks MochiMoppel)
    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%%%02X' "'$c"
        esac
    done
}

urldecode() {
    # urldecode <string>
    local LC_CTYPE=C # fix multibyte characters (thanks MochiMoppel)
    local url_encoded="${1//+/ }"
    printf '%b' "${url_encoded//%/\\x}"
}


slugify.sh
Code:
#!/bin/bash

# usage: slugify.sh "some string"

# converts strings to santised, lower case, dash separated strings with
# whitespace and special characters removed.

# See these links for more info:
#  https://automatthias.wordpress.com/2007/05/21/slugify-in-a-shell-script/
#  https://stackoverflow.com/questions/47050589/create-url-friendly-slug-with-pure-bash
#  https://gist.github.com/oneohthree/f528c7ae1e701ad990e6

slugified="$(echo -n "${@}" | sed -e 's/[^[:alnum:]]/-/g' 2>/dev/null | tr -s '-' | tr A-Z a-z | sed -e 's/-$//g' 2>/dev/null)"
RETVAL=$?

if [ "$slugified" = "" ] || [ $RETVAL -gt 0 ]
then
  slugified="$(echo "$1" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z)"
  RETVAL=$?
fi

[ "$slugified" != "" ] && echo "$slugified"

exit $RETVAL

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK

Last edited by sc0ttman on Wed 30 Jan 2019, 18:01; edited 3 times in total
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Sun 27 Jan 2019, 15:28    Post subject:  

Tutorial: Using SQLite3 databases in Bash CGI scripts

Seems like an easy way to add database support.

Sqlite3 is small, fast, powerful and included in most Puppies by default (I think!).

See near the end.. Skip the awk examples..

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK

Last edited by sc0ttman on Mon 28 Jan 2019, 17:48; edited 1 time in total
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Sun 27 Jan 2019, 16:31    Post subject:  

Puppy without X but with web interface is closer. I can imagine some day Puppy on routers or IoT devices Wink

I wonder if we could use httpd and CGI scripts with mdviewer as a GUI for desktop programs?

Last edited by puppy_apprentice on Sun 27 Jan 2019, 16:36; edited 1 time in total
Back to top
View user's profile Send private message 
rufwoof


Joined: 24 Feb 2014
Posts: 3443

PostPosted: Sun 27 Jan 2019, 16:32    Post subject:  

Really cool sc0ttman. Hadn't even realised busybox contained httpd until I saw this thread. Thanks.
_________________
( ͡° ͜ʖ ͡°) :wq
Fatdog multi-session usb

echo url|sed -e 's/^/(c/' -e 's/$/ hashbang.sh)/'|sh
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Sun 27 Jan 2019, 19:23    Post subject: Haserl  

Even easier (but requires an extra dependency) is Haserl

Haserl is something like a simple, tiny PHP..

Homepage: http://haserl.sourceforge.net/

Tutorial: http://duke-blog.appspot.com/id/20130807094549293

Example:

1. Add this line in /etc/httpd.conf:

Code:
*.hs:/usr/local/bin/haserl
* or whatever the path to 'haserl' is on your system

Scripts ending in .hs should now use Haserl instead of Bash when run by httpd.

2. Now we need a Haserl script to put in the cgi-bin folder:

File: myfile.hs:

Code:
#!/usr/local/bin/haserl
Content-Type: text/html
 
<html>
<body>
 
<% if [ $REQUEST_METHOD == "POST" ]; then %>
 
<%   if [ $POST_twins == "sagill" ]; then %>
correct
<%   else %>
error
<%   fi %>
 
<% else %>
 
<form method="post" action="./test.tt" >
  twins: <input type="text" name="twins"/><br/>
  <input type="submit" value="Submit" />
</form>
 
<% fi %>
 
</body>
</html>



Differences compared to Bash CGI:

* automatic handling of GET and POST vars
* no need to echo the HTML
* much cleaner code

Haserl is available in the Debian repos.. probably in the repos of most other popular distros too.

NOTE:

With some of my code above (parsing GET and POST) as well as some good redirection
stuff (removing the need to 'echo' everything), it should be possible to achieve nice clean Bash CGI
code, making Haserl more of a "nice to have" option.

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1898
Location: Japan

PostPosted: Sun 27 Jan 2019, 23:21    Post subject:  

sc0ttman wrote:
URL encode and decode BASH functions:
Code:

# https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding
urlencode() {
    # urlencode <string>
    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%%%02X' "'$c"
        esac
    done
}

urldecode() {
    # urldecode <string>
    local url_encoded="${1//+/ }"
    printf '%b' "${url_encoded//%/\\x}"
}



Does not work correctly with multibyte characters.
For example the Euro sign € should be encoded to %E2%82%AC. Above code produces only %AC.
Works if you add local LC_CTYPE=C
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Mon 28 Jan 2019, 07:59    Post subject:  

Thanks MochiMoppel.. I knew it wasn't bulletproof.. But that is a nice small fix!
_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Mon 28 Jan 2019, 15:02    Post subject:  

For super fast pages, you could even do it in C or C++

Tutorial: https://linuxconfig.org/simple-cgi-and-apache-examples-on-ubuntu-linux

From the link above:

File: example-c.c
Code:
#include <stdio.h>

int main(void)
{
    printf("Content-Type: text/plain \n\n");
    printf("Hello world \n");
}

Save the code above to ~/www/example-c.c and compile it like so:

Code:
gcc -o ~/www/cgi-bin/example-c example-c.c


Now you should be able to access your C compiled CGI script with:

http://localhost/cgi-bin/example-c

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK

Last edited by sc0ttman on Tue 29 Jan 2019, 08:03; edited 2 times in total
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Mon 28 Jan 2019, 15:29    Post subject:  

Another good resource on BASH CGI scripts: http://www.icosaedro.it/apache/cgi-bash.html (in Italian, but easy enough)

From the link above:

Example of showing an image:

Code:
 #!/bin/bash
echo "Content-Type: image/gif"
echo
cat figura.gif


Example of setting cookies:

Code:
        #!/bin/bash


        function setcookie()
        #
        # $1 = nome variabile
        # $2 = valore variabile
        # $3 = durata (secondi)
        # $4 = path (opzionale)
        #
        {
                value=$( echo -n "$2" | urlencode )
                if [ -z "$4" ]; then
                        path=""
                else
                        path="; Path=$4"
                fi
                echo -n "Set-Cookie: $1=$value$path; expires="
                date -u --date="$3 seconds" "+%a, %d-%b-%y %H:%M:%S GMT"
        }

        # Intestazione MIME:
        echo "Content-Type: text/plain"
        setcookie tuocodice 1234567 60
        setcookie tuonome   "Mario" 60
        setcookie tuocogn   "Rossi" 60
        echo

        # Corpo del messaggio:
        echo "Cookie ritornato dal browser: $HTTP_COOKIE"

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Mon 28 Jan 2019, 16:08    Post subject:  

A simple example using redirection to avoid using lots of echos:

Code:
#!/bin/bash
echo "Content-type: text/html"
echo ""

cat << 'EOF'
<!DOCTYPE html>
<html>
  <head>
    <title>Template</title>
  </head>
  <body>
    <h1>Hello, world</h1>
  </body>
</html>
EOF

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Mon 28 Jan 2019, 16:15    Post subject:  

More examples here: https://github.com/m13253/bash-cgi

These scripts above show how to:
* use caching and some other stuff

-------------------------

BashLib: http://bashlib.sourceforge.net/

Quote:
bashlib is a shell script that makes CGI programming in the bash shell easier, or at least more tolerable. It contains a few functions that get called automatically and place form elements (from POSTs and GETs) and cookies in your environment. It also contains complete documentation on how to use these variables and how to set cookies manually.


This is a great library to help writing CGI scripts.. It has influences many of the others.

Usage:

Quote:
Using bashlib is pretty straight-forward. More important, however, is knowing what to do with the variables once they come into your script and knowing how to write CGI scripts. (This script is not running here, for obvious reasons.)


Code:
#!/bin/bash

# this sources bashlib into your current environment
. /usr/local/lib/bashlib

echo "Content-type: text/html"
echo ""

# OK, so we've sent the header... now send some content
echo "<html><title>Crack This Server</title><body>"

# print a "hello" if the username is filled out
username=`param username`
if [ -n "x$username" != "x" ] ; then
    echo "<h1>Hello, $username</h1>
fi

echo "<h2>Users on `/bin/hostname`</h2>"
echo "<ul>"

# for each user in the passwd file, print their login and full name
# bold them if they are the current user
for user in $(cat /etc/passwd | awk -F: '{print $1 "\t" $5}') ; do
    echo "<li>"
    if [ "$username" = "$user" ] ; then
        echo "<strong>$user</strong>"
    else
        echo "$user"
    fi
    echo "</li>"
done
echo "</ul>"
echo "</body></html>"



-------------------------

Also see cgibashopts: https://github.com/ColasNahaboo/cgibashopts

"A BASH library to parse web forms parameters for bash shell web CGI scripts, even with binary file uploads "

It's a nice library that makes handling GET, POST, and file uploads easier.

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK

Last edited by sc0ttman on Tue 29 Jan 2019, 16:57; edited 1 time in total
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Mon 28 Jan 2019, 19:27    Post subject:  

Bash on Steroids: a simple web framework in Bash

Allows much cleaner code in your CGI files.
(similar to Haserl, above, but in pure Bash - no extra deps)

Homepage: https://github.com/tinoschroeter/bash_on_steroids


Features:

* embed shell code in your HTML using <?bash ... ?> or <? ... ?> tags
* GET and POST vars available as bash variables
* Example: `myfile.cgi?name=bob&age=21` becomes ${name} and ${age}
* use the bos.sh script to build a regular Bash CGI file


Usage:

Create a file like this one:

File: index.htsh
Code:
<!DOCTYPE html>
<html>
 <body>
 <ul>
    <?bash
      for x in John Paul George Ringo; do
        echo "<li>$x</li>"
      done
      ?>
</ul>
</body>
</html>


Then, run the bos.sh script from the same dir as your .htsh file,
the .cgi file that you put in your cgi-bin folder will be generated for you:

Code:
$ ./bos.sh
$ index.htsh --->> /usr/lib/cgi-bin/index.cgi



NOTE: I had to edit the bos.sh files a little to make it work:

1. At the top of bos.sh, I changed the path to my cgi-bin

2. In index.htsh, I changed the form attribute from action="/" to action=""

.. then the generated .cgi files worked for me Smile

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK
Back to top
View user's profile Send private message 
sc0ttman


Joined: 16 Sep 2009
Posts: 2700
Location: UK

PostPosted: Tue 29 Jan 2019, 10:34    Post subject:  

Bash solution for Markdown to HTML:

(from https://gist.github.com/jaburns/33d88f4aa99bb4ab6ad6)

Caution:

This script uses the GitHub API to do the conversion.. Therefore, it requires an internet
connection and you should not convert any Markdown files which contain sensitive or
private information.

Usage:

Code:
md2html.sh <markdown file>   # print HTML to terminal (stdout)
or
Code:
md2html.sh file.md > file.html  # save as HTML file


To use it:

Save the code below as md2html.sh, make it executable.

Code:
#!/usr/bin/env bash

# Markdown to HTML (https://gist.github.com/jaburns/33d88f4aa99bb4ab6ad6)

# NOTE: This script uses the GitHub API to convert the markdown to HTML,
#       and therefore requires an internet connection.

# Usage examples:

#  md2html file.md              # print HTML to stdout
#  md2html file.md > file.html  # create HTML file


# escape slashes and new lines in the given markdown
data="$(
    cat "$1" \
      | sed 's/"/\\"/g' \
      | sed ':a;N;$!ba;s/\n/\\n/g' \
)"

if [[ -z "$2" ]]; then
    context=''
else
    context=",\"context\":\"$2\""
fi


#echo '<!DOCTYPE html>'
#echo '<html>'
#echo '<head>'
#echo '<title>Page Title</title>'
#echo '</head>'
#echo '<body>'

# use the GitHub API to do our conversion for us
curl -s --data "{\"text\":\"$data\",\"mode\":\"gfm\"$context}" 'https://api.github.com/markdown'

#echo '</body>'
#echo '</html>'

unset data
unset context
unset html



...and here's a very simple markdown file to test the conversion:

Save as test.md:

Code:
# Hello

This is a list:

- one
- two

## Iam an h2

Some more para text

```bash
# some bash code
echo "hello"
```

### h3 above table

| foo | bar | baz |
| --- | --- | --- |
| 1   | 2   | 3   |
| 3   | 4   | 5   |

More text.

Another para.

_________________
Pkg, mdsh, Woofy, Akita Linux, VLC-GTK
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [21 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1869s ][ Queries: 12 (0.0382s) ][ GZIP on ]