如何从 shell 脚本文件调用 PHP 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13855666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:10:01  来源:igfitidea点击:

How to call PHP file from a shell script file

phpbashshellexecute

提问by Martin Jacob

I need a shell script which has a loop. In each loop iteration it needs to call a PHP file with some parameters. Is there any way to do it?

我需要一个有循环的 shell 脚本。在每次循环迭代中,它需要调用带有一些参数的 PHP 文件。有什么办法吗?

回答by ericfang

In your php file named test.php, for example

例如,在名为 test.php 的 php 文件中

<?php
//like programs in c language, use $argc, $argv access command line argument number and arguments, uncomment below two line to dump $argc and $argv
//var_dump($argc); //an integer
//var_dump($argv); //an array with arguments
//use args and do anything you want
echo "do my job\n";
exit(0);

then create a shell script named test.sh

然后创建一个名为 test.sh 的 shell 脚本

#! `which bash`
php=`which php`
i=10
while [[ $i -ge 0 ]];
do  
$php test.php 1 2
((i--))
done

put the two files into the same directory. Then run command in the terminal

把这两个文件放到同一个目录下。然后在里面运行命令terminal

bash test.sh

回答by Olaf Dietsche

If this means a Linux/Unix shell

如果这意味着 Linux/Unix shell

for i in `seq 4`; do
    php myscript.php param1 param2
done

But since PHP has loops too, you can do this in PHP as well.

但由于 PHP 也有循环,因此您也可以在 PHP 中执行此操作。

for ($i = 0; $i < 4; $i++)
    system("php myscript.php param1 param2");

回答by Rohit Kumar Choudhary

#!/bin/sh
#
#Script to test for loop
#
#

    while [condition] 
    do
    php test.file
    done