Question

J'ai un script sh qui contient la ligne

$PHP_COMMAND -r 'echo get_include_path();'

Je ne peux pas modifier ce script, mais je dois la ligne de commande éventuelle d'être (équivalent à)

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

Comment puis-je y parvenir?


Voici un script qui illustre le problème.

#!/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 également disponible en ligne: http://gist.github.com/276500

Était-ce utile?

La solution

Il devrait y avoir un moyen plus simple, mais une solution est d'entourer toute la ligne de commande avec des guillemets doubles puis Eval que:

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

Autres conseils

Lecture de vos commentaires sur d'autres réponses, j'ai essayé de déchiffrer ce que vous avez vraiment l'intention de demander:

  

J'ai un script sh qui contient la ligne

$PHP_COMMAND -r 'echo get_include_path();'
     

Je ne peux pas modifier ce script, mais je dois la ligne de commande éventuelle d'être (équivalent à)

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

Comment puis-je y parvenir?

Si cela reflète précisément votre situation, c'est une façon:

Ecrire un autre script (je vais l'appeler /some/path/to/bar.sh) à utiliser comme PHP_COMMAND. Étant donné que la variable n'est pas cité dans le script original, vous devrez vous assurer que le chemin complet bar.sh n'a pas de caractères shell-spéciaux (comme les espaces).

/some/path/to/bar.sh

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

Ensuite, pour l'exécuter, définissez PHP_COMMAND et exécutez le script original (/path/to/foo.sh):

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

Le printf Bash a quelque chose de magique secret spécial qui pourrait aider. Essayez:

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

ou une variante de cela.

Modifier

Je suis désolé, j'ai inclus quelques explications et un exemple. Les causes de drapeau de %q printf pour ajouter échapper à la chaîne de sorte qu'il peut être réutilisé dans une autre commande. La sortie de cette printf ressemblerait à ceci (les guillemets simples se sont échappés):

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

Cette commande illustre une Escaping supplémentaire:

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

Qu'en est-ce?

$PHP_COMMAND ' -r echo get_include_path();' 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top