Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a shell script with an html button

Tags:

html

shell

button

I want to launch a bash script when a button is pressed on a website. This is my first attempt:

<button type="button" onclick="/path/to/name.sh">Click Me!</button> 

But no luck. Any suggestions?

like image 666
dukevin Avatar asked Jun 04 '11 09:06

dukevin


People also ask

How do you call a bash script from HTML?

We can run a bash script using the HTML button in the browser using the shell_exec() or exec() or system() functions. The three functions will return the same value if we use these functions. The differences are: shell_exec() returns the complete output as a string.

What is $() in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What is $_ in shell script?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


1 Answers

As stated by Luke you need to use a server side language, like php. This is a really simple php example:

<?php if ($_GET['run']) {   # This code will run if ?run=true is set.   exec("/path/to/name.sh"); } ?>  <!-- This link will add ?run=true to your URL, myfilename.php?run=true --> <a href="?run=true">Click Me!</a> 

Save this as myfilename.php and place it on a machine with a web server with php installed. The same thing can be accomplished with asp, java, ruby, python, ...

like image 176
nsg Avatar answered Sep 19 '22 02:09

nsg