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?
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.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
$_ (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.
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, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With