我需要为 PHP 解释器创建一个包装脚本(已在 PATH 中):
#!/bin/bash
# This is a wrapper for the PHP interpreter.
#
# We prepare the enviroment for SQL Anywhere PHP extension sourcing
# /opt/sqlanywhere17/bin64/sa_config.sh and then we call the PHP
# interpreter already in the PATH passing -dextension=sqlanywere.so which
# enable the PHP extension.
#
# Don't enable sqlanywhere.so globally becase for some reason this will
# break the command "plesk bin php_handler". In addition the extension
# requires the SQLANY17 environment variable which is hard to set with
# CGI/FastCGI at this time.
php $@
但是我的php-wrapper
无法正确处理引号。例如,这在原始解释器中有效:
php-r 'echo "Works";'
php-wrapper
使用相同的参数运行:
./php-wrapper -r 'echo "Works";'
PHP 解析错误:语法错误,命令行代码第 1 行出现意外文件结束
我如何调试参数$@
来了解发生了什么以及如何解决这个问题?
答案1
用$@
双引号括起来,因此:
php "$@"
实际情况是, 的参数php-wrapper
是-r
和echo "Works";
which ,没有问题。但是,由于$@
未加引号,因此php-wrapper
接收的所有参数都被解析为单独的单词,因此php
被传递-r
、echo
和"Works"
。和双引号中的$@
,php
将被传递-r
和echo "Works"
。
从bash
手册页:
@ Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$@" is equivalent to "$1"
"$2" ...
当参数解析没有按照我想要的方式进行时,我会使用一个小脚本:
$ cat echoargs
#!/bin/bash --
count=0
for i in "$@"
do
echo "$count: $i"
((count++));
done
将您的包装脚本更改为:
echoargs $@
echo ====
echoargs "$@"
然后尝试对包装器进行各种调用,以查看它传递了什么。