[英]How can execute php scripts in bash?
I need parse uri in shell scripts. 我需要在shell脚本中解析uri。 So, I tried to use php in bash as below.
所以,我尝试在bash中使用php,如下所示。
#!/bin/sh
uri="http://www.google.com?key=value
key="host"
value=$(php -r "$parse = parse_url('$uri'); echo $parse['$key']")
It has showing the following error. 它显示以下错误。
PHP Parse error: syntax error, unexpected '=' in Command line code on line 1
Some body can help how to use embedded php in bash ? 有些身体可以帮助如何在bash中使用嵌入式php?
A cheap way of debugging this is to use echo
to see what you're passing in to php: 一种廉价的调试方法是使用
echo
来查看你传递给php的内容:
echo "$parse = parse_url('$uri'); echo $parse['$key']"
shows 节目
= parse_url('http://www.google.com?key=value'); echo ['host']
You're already using $uri
to mean "the value of the shell variable uri", so it's not surprising that $parse
is also considered a shell variable and expanded to its value (unset, nothing). 你已经使用
$uri
来表示“shell变量uri的值”,所以$parse
也被认为是一个shell变量并扩展到它的值(未设置,没有)也就不足为奇了。
Use \\$
when you want a literal dollar sign in your double quoted string: 如果要在双引号字符串中使用文字美元符号,请使用
\\$
:
value=$(php -r "\$parse = parse_url('$uri'); echo \$parse['$key']")
You can use it easily, but you must be careful because of escaping in bash. 你可以轻松使用它,但你必须小心,因为在bash中逃脱。
I recommend to use single quotes (you do not need to escape anything) and exit from the quotes when you want to do something special. 我建议使用单引号 (您不需要转义任何内容)并在想要执行特殊操作时退出引号。 Your example:
你的例子:
php -r '$parse=parse_url("'$url'"); echo $parse["'$part'"];'
Note that 注意
$parse
$parse
'$url'
'$url'
"
instead. "
来代替。 Update: 更新:
Just for the clarification - parse error happened because $parse
was interpreted as bash variable (empty string) so the php command incorrectly started with =
. 只是为了澄清 - 解析错误发生,因为
$parse
被解释为bash变量(空字符串)所以php命令错误地以=
开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.