2

I need to create a wrapper script for PHP interpreter (already in 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 $@

My php-wrapper however is not working properly with quotes. For example, this works with the original interpreter:

php-r 'echo "Works";'

Running php-wrapper with the same arguments:

./php-wrapper -r 'echo "Works";'

PHP Parse error: syntax error, unexpected end of file in Command line code on line 1

How can I debug arguments $@ to see what's happening and how can I solve this problem?

2

Wrap your $@ in double quotes, thusly:

php "$@"

What's happening is that the arguments to php-wrapper are -r and echo "Works"; which is fine. However, because $@ isn't quoted, all the arguments php-wrapper receives are parsed as separate words, so php is being passed -r, echo, and "Works". With the double-quoted $@, php will be passed -r and echo "Works".

From the bash manual page:

   @      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"  ...

Here's a little script I use when argument parsing isn't happening the way I want it to:

$ cat echoargs
#!/bin/bash --

count=0

for i in "$@"
do
    echo "$count: $i"

    ((count++));
done

Change your wrapper script to:

echoargs $@
echo ====
echoargs "$@"

and then try various invocations of the wrapper to see what it's passing on.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.