# execute shell script using CGI for html site

**URL:** <https://community.unix.com/t/execute-shell-script-using-cgi-for-html-site/286337>\
**Category:** UNIX and Linux Applications\
**Created:** [April 14, 2011, 1:30am UTC](https://community.unix.com/t/execute-shell-script-using-cgi-for-html-site/286337 "2011-04-14T01:30:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheshe](https://community.unix.com/letter_avatar/sheshe/32/6_449c533f586939b6e5f246632a46421f.png) [@sheshe](https://community.unix.com/u/sheshe)\
**Post date:** [April 14, 2011, 1:30am UTC](https://community.unix.com/t/execute-shell-script-using-cgi-for-html-site/286337/1 "2011-04-14T01:30:42Z")

</div>

hi there  
im currently in the process of creating a website for use basically within our org. im using a os x machine and installed MAMP - which includes Apache, mysql... the site will be used by techs to primarily install pkgs files onto os x devices. i would like to have buttons or hyperlinks (doesnt matter) that when the tech selects the object (e.g. a pkg file - adobe reader)... a sh script is executed that will basically install the app onto the os x device. i have placed a test shell script in the cgi-bin folder and changed ownership so its executable (chmod +x scriptname).  
the test script looks like this:  
#!/bin/sh  
echo "content-type: text/html"  
""  
mkdir /users/test/thisisatestfolder

in the html page - i dont know how i would "call it" , e.g. form etc...  
i dont even know if the script is set-up right as well  
any help would be great  
thanks!

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/6_449c533f586939b6e5f246632a46421f.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [April 14, 2011, 9:32am UTC](https://community.unix.com/t/execute-shell-script-using-cgi-for-html-site/286337/2 "2011-04-14T09:32:30Z")

</div>

The base shell and web service are not too compatible, both because shell needs help decoding URLencoded strings and because it is easy to open security holes (shell injection, I guess you could call it) with metacharacters in input. You can do some simple things, especially when the user input is not required, like a report with no inputs and no forms, just information or links.

The rules of CGI are pretty simple, and executables in you cgi-bin subtree will get called by the web server. Scripts may not act like they do interactively, since there is no tty. Basicly, you write to stdout a good http header minus the first line, blank line and whatever document you want, then exit 0. Get input is by env var., and post by read-it-yourself from stdin. There are a handful of support variables. You can see them with this cgi script:

```nohighlight
#!/usr/bin/ksh
 
echo "Content-type:text/plain
 
CGI Environment:
'
 
set 
 
echo '
---- END ----'
 

```

---

<div class="post-metadata">

**Author:** ![sysgate](https://community.unix.com/user_avatar/community.unix.com/sysgate/32/3504_2.png) [@sysgate](https://community.unix.com/u/sysgate)\
**Post date:** [April 14, 2011, 9:47am UTC](https://community.unix.com/t/execute-shell-script-using-cgi-for-html-site/286337/3 "2011-04-14T09:47:33Z")

</div>

This could be dangerous, but generally it will depend on what scripting language you are using, for example with PHP behind the scenes you could use system(); / exec(); calls, with Perl/CGI you could use the same commands, as long as you put your script in executable directory, as in:

```nohighlight
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Bash as CGI"
echo "</title></head><body>"

echo "<h1>Hello world</h1>"
echo "Today is $(date)"

echo "</body></html>"

```

and all this is enclosed in a script that has executable rights, most of the times placed in folder like "cgi-bin".
