Question

I am creating a simple web page that will allow the user to select an option on a basic php page, click submit, and then the backend PHP should execute a bash script, that will execute some AppleScript.

I can run the bash script from Terminal and it runs the Applescript file fine using osascript but when I try executing the bash script from php, it will run echo commands, but will not execute the osascript line. I imagine this is a permissions issue, but I changed everything to full privilege using chmod 777 (admittedly not the best idea, but that is besides the point)

Is there something I am missing? It is weird that it seems to skip completely over the osascript line. Any suggestions? Thanks in advance!

CODE:

PHP

<?php
if (isset($_POST['submit'])) {
    $username =  $_POST['username'];
    if (strcasecmp($username, 'user1') == 0) {
        echo 'test1';
    } else if (strcasecmp($username, 'user2') == 0) {
        echo 'test2';
    } else if (strcasecmp($username, 'user3') == 0) {
        echo 'test3';
    } else if (strcasecmp($username, 'user4') == 0) {
        shell_exec('cd ~/Sites;./a.out');
    }
}
?>

BASH

#!/bin/bash
echo "test String"
cd /Users/Zimmerman/Library/Scripts
osascript /Users/Zimmerman/Library/Scripts/testscript.scpt
echo "test String"
Was it helpful?

Solution

Your apache $PATH variable is probably not looking within /usr/bin/. If you echo shell_exec('which osascript'); within a PHP file, you might not see anything, but you will see results on terminal.

You can add paths to your webservers $PATH variable or you could perhaps try using the full path to the executable /usr/bin/osascript on your scripts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top