Pregunta

Tengo un script sh que contiene la línea

$PHP_COMMAND -r 'echo get_include_path();'

No puedo editar este script, pero necesito la línea de comandos eventual ser (equivalente a)

php -d include_path='/path/with spaces/dir' -r 'echo get_include_path();'

¿Cómo puedo lograr esto?


A continuación se muestra un script que muestra el problema.

#!/bin/sh

# shell script quoting problem demonstration

# I need to be able to set a shell variable with a command with 
# some options, like so
PHP_COMMAND="php -d 'include_path=/path/with spaces/dir'"
# then use PHP_COMMAND to run something in another script, like this:
$PHP_COMMAND -r 'echo get_include_path();'
# the above fails when executed. However, if you copy/paste the output
# from this line and run it in the CLI, it works!
echo "$PHP_COMMAND -r 'echo get_include_path();'"
php -d include_path='/path/with spaces/dir' -r 'echo get_include_path();'
# what's going on?

# this is also interesting
echo "\n--------------------"

# this works great, but only works if include_path doesn't need quoting
PHP_COMMAND="php -d include_path=/path/to/dir"
echo "$PHP_COMMAND -r 'echo get_include_path();'"
$PHP_COMMAND -r 'echo get_include_path();'

echo "\n--------------------"

# this one doesn't when run in the sh script, but again if you copy/paste 
# the output it does work as expected.
PHP_COMMAND="php -d 'include_path=/path/to/dir'"
echo "$PHP_COMMAND -r 'echo get_include_path();'"
$PHP_COMMAND -r 'echo get_include_path();'

Script también disponible en línea: http://gist.github.com/276500

¿Fue útil?

Solución

No debe haber una manera más sencilla, pero una solución es para rodear toda la línea de comandos con comillas dobles y luego eval que:

PHP_COMMAND="php -d 'include_path=/path/with spaces/dir'"
eval "$PHP_COMMAND -r 'echo get_include_path();'"

Otros consejos

La lectura de sus comentarios sobre otras respuestas, he tratado de descifrar lo que realmente la intención de preguntar:

  

Tengo un script sh que contiene la línea

$PHP_COMMAND -r 'echo get_include_path();'
     

No puedo editar este script, pero necesito la línea de comandos eventual ser (equivalente a)

php -d include_path='/path/with spaces/dir' -r 'echo get_include_path();'
     

¿Cómo puedo lograr esto?

Si esto refleja con precisión su situación, entonces esta es una manera:

Escribir otro script (lo llamaré /some/path/to/bar.sh) para su uso como PHP_COMMAND. Dado que la variable no es citado en el guión original, tendrá que asegurarse de que el camino completo del bar.sh no tiene ningún carácter especial de concha (como espacios).

/some/path/to/bar.sh

#!/bin/sh
exec php -d 'include_path=/path/with spaces/dir' ${1+"$@"}

A continuación, para ejecutarlo, fijar PHP_COMMAND, y ejecutar el guión original (/path/to/foo.sh):

env PHP_COMMAND=/some/path/to/bar.sh '/path/to/foo.sh'

printf de Bash tiene algo de magia secreto especial que podría ayudar. Proveedores:

PHP_COMMAND="php -d $(printf "%q" "'include_path=/path/to/dir'")"

o alguna variación de eso.

Editar

Lo siento, debería haber incluido alguna explicación y un ejemplo. Las causas bandera %q printf añadir escapar a la cadena para que pueda ser reutilizada en otro comando. La salida de ese printf sería el siguiente (las comillas simples consiguen escaparon):

\'include_path=/path/to/dir\'

Este comando ilustra algunas escape adicional:

$ printf "%q" "string ';\ text"
string\ \'\;\\\ text

¿qué pasa con esto?

$PHP_COMMAND ' -r echo get_include_path();' 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top