在php中的shell或从php发送变量到shell

问题描述 投票:-1回答:1

我正在编写一个PHP脚本,我必须在其中运行shell脚本,

我有2个选项:

  1. 如果我能够将php变量值发送到shell
  2. 或者我可以直接在PHP中编写shell

我用了

shell_exec(dirname(__FILE__) ."/shl.sh");

for execute shell ....现在问题是..如果我在shell中使用#!/ usr / bin / php它只解析<?php ?>中的代码并直接在屏幕上打印shell语句。

php shell
1个回答
1
投票

最好的选择(到目前为止!)将修改外部脚本以接受命令行参数。

而不是

shell_exec('sh.sh');

嵌入所有变量的地方,将其变为

shell_exec("./sh.sh $opt1 $opt2");

您可以在哪里轻松传递变量。

使用这些参数的bash脚本的示例是:

#!/bin/bash
echo "My $1 will kick your $2 anytime"

它将用第一个参数替换$ 1,用第二个参数替换$ 2。

© www.soinside.com 2019 - 2024. All rights reserved.