bash 脚本循环遍历数组并使用 arg 运行 PHP 脚本



我有一个 bash 脚本,它每天凌晨 1:01 在 cron 中运行,bash 脚本是:

array_of_clients=(2 187 317 927 1863 2993 3077 3440 3444 3457 3459 3469 3484 3487 3494 3497 3522 3544 3551 3553)
for i in "${array_of_clients[@]}"
do
    echo "nRunning Client - $i"
    php -f "/mnt/www/bin/scheduled/import_client.php" $i
    echo "nFinished Client - $i"
done

这个问题是我不知道$i是否作为参数传递给 php 脚本。我做错了什么吗?如果我将$i放在"中,它说它找不到该文件,因为文件名变成/mnt/www/bin/scheduled/import_client.php 2

例如

谁能帮忙?

您可以在预定义的全局变量 $argv 中访问 PHP 脚本中的命令行参数。在这种情况下,您的$i将被视为$argv[1]

试试这个脚本:

<?php
global $argv;
var_dump($argv);
?>

php -f test.php A B C defgh收益运行它:

array(5) {
  [0]=>
  string(8) "test.php"
  [1]=>
  string(1) "A"
  [2]=>
  string(1) "B"
  [3]=>
  string(1) "C"
  [4]=>
  string(5) "defgh"
}

最新更新