1

I am trying to run a bash script from an html file using php. I've read this question (how to run a .sh file from php?) and followed it exactly but cannot seem to get it to work.

I have two files in my downloads folder on my mac. These are the file names and their content:

hello.sh

echo hello world

test.html

<?php
echo shell_exec('sh /Users/fred/downloads/thing.sh');
?>

I ran this command in command line while in the downloads folder:

open test.html

This resulted in a blank page being opened in chrome while I was expecting to get a page with "hello word" in it.

Thanks for the help.

2
  • Btw: bash != sh. I replaced bash tag by sh tag. Commented Jun 18, 2016 at 5:56
  • Try to use exec function insted of shell_exec. exec can return the status of the executed command. Commented Jun 18, 2016 at 7:43

2 Answers 2

1

rewrite your bash file in nano as

#! /bin/bash

echo "hello world"

In the command line type "bash hello.sh" to execute, this will test if it is working or not.

test.html rewrite to test.php php code only work with *.php extension.

Sign up to request clarification or add additional context in comments.

Comments

0

rename 'test.html' to 'test.php'. also check the file name here

echo shell_exec('sh /Users/fred/downloads/thing.sh');

i think this should be

echo shell_exec('sh /Users/fred/downloads/hello.sh');

If again you face the same issue then may be your apache server doesn't have permission to access 'hello.sh'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.