从 bash 执行 php 脚本,将输出分配给 bash 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11051064/
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-09-18 02:30:29  来源:igfitidea点击:

Execute php script from bash , assign output to a bash variable

phpbash

提问by sakhunzai

I have a bash script which need to execute some php scripts and to get back the results e.g

我有一个 bash 脚本,它需要执行一些 php 脚本并返回结果,例如

#!/bin/bash
/usr/bin/php -f $HOME/lib/get_fifobuild.php

The script get_fifobuild.php returns an integer which I need to assign into a bash variable. I ll appreciate if someone help me out.

脚本 get_fifobuild.php 返回一个整数,我需要将其分配给 bash 变量。如果有人帮助我,我将不胜感激。

thanks :)

谢谢 :)

Edit:php show.php

编辑:php show.php

<?php 
  echo phpinfo();
  exit;
?>

bash script:

bash脚本:

#!/bin/bash
HOME=`dirname 
Date: Fri Jun 15 19:16:00 PKT 2012
phpinfo()
`; log(){ NEW_LOG=$HOME/logs/cloud-`date +%d_%m_%Y`.log echo >> $NEW_LOG } log "Date: `date`"; data=$(/usr/bin/php -f $HOME/lib/show.php); log $data;

output:

输出:

myvariable=$(/usr/bin/php -f $HOME/lib/get_fifobuild.php)

no luck yet

还没有运气

采纳答案by John Lawrence

<?php
    phpinfo();
?>

Will assign the output from your php script to a variable called "myvariable".

将您的 php 脚本的输出分配给一个名为“myvariable”的变量。

Update:

更新

This will assign the output of the command to the variable, but as you are still having problems I can perhaps suggest a few things:

这会将命令的输出分配给变量,但由于您仍然遇到问题,我或许可以提出一些建议:

  1. you have 'get_builds.php' and 'get_fifobuild.php' elsewhere.

  2. check that $HOME is being set correctly. You may be better with a different variable name here as that environment variable generally is set to your home directory. This however is unlikely to be the problem as you are getting output from the script.

  3. Is the text you gave the exactcontents of your PHP file? If you have quotes around phpinfo()for example it will cause the output to just be the string "phpinfo()". In fact, you do not need the echoat all and could make the contents of your PHP file as follows.

  1. 您在其他地方有“get_builds.php”和“get_fifobuild.php”。

  2. 检查 $HOME 是否设置正确。在这里使用不同的变量名称可能会更好,因为该环境变量通常设置为您的主目录。但是,这不太可能是问题,因为您正在从脚本中获取输出。

  3. 您提供的文本是您的 PHP 文件的确切内容吗?phpinfo()例如,如果您有引号,它将导致输出只是字符串“phpinfo()”。实际上,您根本不需要echo,可以按如下方式制作 PHP 文件的内容。

get_fifobuild.php:

get_fifobuild.php:

#!/bin/bash
HOME=`dirname 
foobar=`/usr/bin/php -f $HOME/lib/get_fifobuild.php`
`; log(){ NEW_LOG=$HOME/logs/cloud-`date +%d_%m_%Y`.log echo "" >> $NEW_LOG } log "Date: `date`"; data=$(/usr/bin/php -f $HOME/lib/show.php); log "$data";

Update 2:

更新 2:

Try changing your script to:

尝试将您的脚本更改为:

##代码##

Basically adding double quotes around the variables in the 'log' and 'echo' lines. The problem you were having was that only the first line of your php output was being logged.

基本上是在 'log' 和 'echo' 行中的变量周围添加双引号。您遇到的问题是仅记录了 php 输出的第一行。

回答by Rufinus

##代码##

note: these are backticks.

注意:这些是反引号。