How to run simple shell scripts directly from a browser?

To be able to run shell scripts directly from the browser, you should add this rule to your .htaccess file:

Options +ExecCGI
AddHandler cgi-script .sh

This tells the web server to treat .sh files as CGI scripts.

To test this, let's try printing today's date in your browser. To do this, create a file called date.sh with the following content:

#!/bin/bash
DATE="$(date)"
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Test</title></head><body>"
echo "Today is $DATE <br>"

Save the file and change its permissions to 755.

Now open www.yourdomain.com/date.sh and the output you get should be similar to this:

Today is Sun Jan 21 09:01:38 CST 2018

You find this article useful? Click here to learn more about SiteGround web hosting experts and what else we can do for you!