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

#! /bin/bash
echo hello world

test.php

<?php
echo shell_exec('sh /Users/steve/downloads/hello.sh');
?>

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

php test.php

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

I am also wondering how to get my php code to open a window in chrome like html files do. For example, if I enter this into command line:

open test.html

a new window is opened in chrome running the content of test.html.

Thanks for the help.

Community
  • 1
  • 1
steve
  • 29
  • 3
  • Have you installed Apache serve and php on your system ? – Maulik Kanani Jun 18 '16 at 07:20
  • @AniMenon : Why should they? – sjsam Jun 18 '16 at 08:06
  • Couldn't figure out the problem. But could you try `chmod +x hello.sh` and `echo shell_exec('./hello.sh');` ? – sjsam Jun 18 '16 at 08:13
  • @ Maulik Kanani, no unless they come with mac. – steve Jun 18 '16 at 08:17
  • @sjsam, should I put chmod +x hello.sh in command line? – steve Jun 18 '16 at 08:19
  • @steve : Yeah . From inside the `downloads` folder. – sjsam Jun 18 '16 at 08:20
  • @sjsam, just did what you said and it worked great! thanks sooo much! you should post that as an answer. – steve Jun 18 '16 at 08:24
  • @sjsam, I'd also like to ask how to run a php file in chrome. For example with test.html if I enter "open test.html" in command line a new page opens in chrome with the content of test.html. is there a way to do this with a .php? – steve Jun 18 '16 at 08:27
  • @steve : Most probably a permission issue while traversing `/Users/steve/downloads/`. You may experiment a bit and post an answer. – sjsam Jun 18 '16 at 08:31
  • @sjsam, I'll post an answer tomorrow, do you know how to make a php open in chrome like html does? – steve Jun 18 '16 at 08:34
  • Running a php file in chrome means you should access the file php file through test/local domain configured with apache. Say if your test domain is `test.web` your could access the file like `test.web/your_file.php` If your apache server has mod_php enabled or has access to php via CGI(you should check this), then apache automatically handle php files using the php module.. – sjsam Jun 18 '16 at 08:37
  • so on my computer I can't run a .php like I can with a .html? – steve Jun 18 '16 at 08:40
  • @sjsam now whatver you just put up "Apache" that's a server. And thats exactly what I said earlier.. – Ani Menon Jun 18 '16 at 08:58
  • @AniMenon : Okay. I thought that was regarding first/main part of the question.. This is exactly why we discourage multiple questions consolidated to one. – sjsam Jun 18 '16 at 09:39

1 Answers1

1

Here is an example of how you can create a .php file named "test.php" that runs a shell script named "hello.txt".

This is the code that goes inside "test.php". You can put this code in any directory and it should work fine.

<?php
echo shell_exec('./hello.sh');
?>

This is the code that goes inside "hello.sh". You can put this code in any directory so long as it is in the same directory as "test.php".

#! /bin/bash
echo hello world

Once you have made both these files, go into their directory using command line and enter the following commands:

chmod +x hello.sh
php test.php

The first command (I think) makes "hello.sh" accessible from "test.php". You only need to do this once. The second command runs test.php and should return "hello world".

This can also be used with programs other than shell scripts. For example you could compile a cpp file into a.out (the default name of a compiled c++ file) and run it from the php file by replacing echo shell_exec('./hello.sh'); with echo shell_exec('./a.out');

steve
  • 29
  • 3