简体   繁体   English

在php中运行.sh脚本

[英]Run a .sh script in php

I'm trying to run a .sh script in php. 我正在尝试在php中运行.sh脚本。

Here's the sh code 这是sh代码

 php fetchdrive.php

When I run the .sh script by double clicking, it works fine, and displays the variables I've returned (an array). 当我双击运行.sh脚本时,它可以正常工作,并显示我返回的变量(数组)。 I'm using wampserver to run php. 我正在使用wampserver运行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. 多个php文件可以正常工作,不幸的是,该文件只能在命令行中运行,因此我想出了为什么不通过index.php文件中的sh调用命令的原因。 When I try <?php echo exec('sh C:/wamp64/www/run.sh'); ?> 当我尝试<?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. <?php echo exec('sh C:/wamp64/www/run.sh'); ?>在我的index.php文件中,它不起作用。 I know this because at the bottom of the php file I'm trying to execute it has alert("Hello"); 我知道这一点,因为在php文件的底部,我试图执行它具有alert(“ Hello”);。

Infact I don't think it's running the command at all when I try to use echo exec. 实际上,当我尝试使用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. 所有这些都是因为我试图运行命令“ php fetchdrive.php”而不必在终端中手动键入该命令,因为当我开始运行该服务器时,不必每次都重新运行该命令。

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. 它适用于我的其他php文件。

tl;dr I'm just trying to run a php terminal command within a website. tl; dr我只是想在网站内运行php终端命令。

Here is what I did, so if it doesn't work for you then you may have some problem with WAMP or something. 这是我所做的,因此,如果对您不起作用,则可能是WAMP或其他问题。 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): 和sh文件(password_hash函数至少需要PHP v5.5):

#!/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. 还有一种可能是您的bash并非真正位于/ bin / bash。 In your terminal type "which bash" and see what it says. 在您的终端中,键入“哪一个bash”,并查看其内容。 Modify your shebang as needed. 根据需要修改您的shebang。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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