在shell脚本中执行php脚本?
问题描述:
我想在shell脚本中调用一个php脚本,并且需要发送4个参数。我打电话给这样的shellcript内部调用。在shell脚本中执行php脚本?
php /var/www/php/myscript.php $var1 $var2 $var3 $var4
但php脚本没有执行。那么将参数发送到php脚本并在shell脚本中执行脚本的正确方法是什么?
答
假设你有一个可执行文件file.sh
:
#!/usr/bin/php
<?php
include('your/file.php');
exit;
或与当前环境
#!/usr/bin/env php
<?php
include('your/file.php');
exit;
然后用
$ ./file.sh
答
执行它的命令行上这是样品,要特别注意参数中的引号来包装任何字符像空格或破折号这样的字符串会影响参数的读取。
bash.sh
#!/bin/bash
php test.php "$1" "$2"
test.php的
<?php
$a = $argv[1];
$b = $argv[2];
echo "arg1 : $a, arg2: $b";
某些产出
$bash bash.sh Hello World
arg1 : Hello, arg2: World
$bash bash.sh Hello
arg1 : Hello, arg2:
$bash bash.sh "Hello World"
arg1 : Hello World, arg2:
希望这有助于
不要忘记正确的权限添加到该文件,如果你想通过问题执行它“./file。 sh“ 例如 chmod + x file.sh –