简体   繁体   English

执行shell脚本代码取决于php返回值

[英]execute shell script code depends upon php return value

I haven't that much of knowledge in PHP, and I need to run a script that depends upon the PHP return value. 我对PHP的了解不多,我需要运行一个依赖于PHP返回值的脚本。

script 脚本

presentDir=`pwd`
success=$(php -f $presentDir/optimize.php $file)
echo echo "value:".$success

PHP code PHP代码

<?php
try{        
    //code  
} catch(Exception $e){
    return 1
}
return 0

When executing the above shell script, success is empty. 执行上面的shell脚本时,成功为空。

PHP's return statement doesn't send any output to STDOUT, which is what you'd be catching with the $() construct. PHP的return语句不会将任何输出发送到STDOUT,这是您使用$()构造要捕获的内容。 To see the exit value from a command you use the exit statement in PHP and the shell's $? 要查看命令的退出值,请使用PHP中exit语句shell的$? variable . 可变的 Maybe this is what you're looking for? 也许这就是您要找的东西?

<?php
try {
    //code
} catch(Exception $e){
    exit(1);
}
exit(0);
?>

Followed by: 其次是:

#!/bin/sh
php -f optimize.php "$file"
printf "PHP script returned %d" $?

# or you could just use if:
if php -f optimize.php "$file"; then
    do_stuff
fi

There's no need to include the current working directory in a script call. 无需在脚本调用中包括当前工作目录。 (Further to that, you could make the script executable and call it directly!) (此外,您可以使脚本可执行并直接调用!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM