I want to run a shell script from a php-based website on my localhost (using MAMP on Mac) but it does not work, unfortunately.
Heres's the shell script:
#!/bin/bash
open /Users/my_username/Desktop/aiSee.app
If I run it from the terminal, it works fine and opens the app. Using this code in my website, it does not work:
<?php
echo exec('script.sh');
?>
No errors or something are displayed, it just doesn't work.
The script is located at the same source as the .php file for the website.
open in bash does not do what you think it does. You need cat instead:
#!/bin/bash
cat /Users/my_username/Desktop/aiSee.app
Has your issue been solved?
If not, try echo exec('sh script.sh'); or echo exec('bash script.sh');.
This question may solve your problem: how to run a .sh file from php?
Related
I just started with php and I installed my xampp and got it running. Now I wanna run my code that I have, its a simple print function but when I try running it, I see the code appear in Chrome an not the print. this is what I have in my code: <?php echo "Hello World!"; ?> and when I try to run it I get the exact same thing on chrome
You should start apache and MySQL if you are connecting to DB on XAMPP then put your script in the htdocs folder then you can run your code by going to http://localhost
Another solution is by running this command to start a php server
php -S 127.0.0.1:8000 index.php
I'm using XAMPP on Windows 10, and trying to run a simple PHP script that uses the shell_exec function to call a simple R script in the same directory. Here's the PHP index.php file:
<?php
echo shell_exec('Rscript test.R');
And here's the test.R file:
print("Hello, World!")
From the command line (i.e., Git for Windows), when I run php index.php in the folder, I get the following output:
[1] "Hello, World!"
However, when I run index.php from the web browser via XAMPP and localhost, I don't see anything on the page. At first, I thought it might be a permissions issue, so I gave Full control access to all users on the folder, but it didn't seem to change anything.
Any ideas on how to get this working from the browser? Thank you.
Found the answer to the problem I was having. Apparently, even though Rscript is part of my Windows 10 PATH variable, when I try to execute an R script via shell_exec in PHP, I have to have the whole path to the Rscript executable (I guess because the PHP script / Apache server doesn't know about the path variable).
I changed the shell_exec function call as follows, and it worked:
echo shell_exec('"C:\Program Files\R\R-4.1.0\bin\Rscript" test.R');
Here's a link to the SO post that helped me figure this out:
shell_exec does not execute from browser requests
I'm trying to run a .sh script in php.
Here's the sh code
php fetchdrive.php
When I run the .sh script by double clicking, it works fine, and displays the variables I've returned (an array).
I'm using wampserver to run php. Multiple php files work properly, unfortunately this one can only be run command line, so I figure why not call the command via sh in my index.php file. When I try <?php echo exec('sh C:/wamp64/www/run.sh'); ?> in my index.php file, it doesn't work. I know this because at the bottom of the php file I'm trying to execute it has alert("Hello");
Infact I don't think it's running the command at all when I try to use echo exec. All of this is because I'm trying to run a command "php fetchdrive.php" without having to manually type it in to terminal, since when I start running this server I won't have to re-run the command every time.
Any ideas?
Edit: Here's the script tag for the alert
<script type="text/javascript"> alert("Hello!"); </script>
It works on my other php files.
tl;dr I'm just trying to run a php terminal command within a website.
Here is what I did, so if it doesn't work for you then you may have some problem with WAMP or something. I might be wrong, but I don't think you're on a box that has real bash.
<?php
passthru(__DIR__ . '/lame.sh');
And the sh file (requires at least PHP v5.5 for the password_hash function):
#!/bin/bash
PASSWORD = 'kookoopapa1'
PHP=`which php`
PASSWORD=`$PHP -r "echo password_hash( '$PASSWORD', PASSWORD_BCRYPT, ['cost' => 11] );"`
printf "<script type=\"text/javascript\"> alert(\"$PASSWORD\"); </script>\n"
Give it a shot. There's also a chance that your bash is not really at /bin/bash. In your terminal type "which bash" and see what it says. Modify your shebang as needed.
I'm using a Raspberry Pi as a web server. I use ouimeaux on it, so when I want to turn it on, I use wemo switch name on.
I've got a PHP file called 'wemo-on.php' which consists of:
<?php
echo exec('wemo switch name on');
echo 'OK';
?>
However, when I browse to http://pi/wemo-on.php, I get OK but the script doesn't run at all. Just OK.
Things I've tried:
Changing the exec() command to sudo wemo switch name on
chmod +x wemo-php
chmod 777 wemo.php
Creating a batch file wemo-on.sh and calling that instead of the command itself
Still not working... :(
Any advice? I'm not exactly a pro at PHP/Linux, but I have been able to use php exec() on a Windows machine before without any drama.
In my php code:
exec(test.sh);
and test.sh has code:
echo "Hi this is test?" | espeak --stdout > demo.wav
But nothing happen. No error, No output.
If i try to execute test.sh from terminal that it will work perfectly. So why it not run on my php.
Can someone help me?
How do you run your PHP script? With php-cli (in shell mode)? HTTP (Apache, ...)?
It could be a path problem. Could you give us the path of your test.sh, the path of your php script or the URI which is called to run the PHP if you use apache or other HTTP server?
the apache user will need write access to the current working directory (presumably the same directory that contains your php script and test.sh).