简体   繁体   中英

Run a .sh script in php

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'); ?> <?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM