我想在shell脚本中调用一个php脚本,需要发送4个参数。我把它叫做这样的内部shell。
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:
希望这能帮助