مشكلة الاقتباس المتغير لبرنامج Shell النصي

StackOverflow https://stackoverflow.com/questions/2061901

  •  20-09-2019
  •  | 
  •  

سؤال

لدي برنامج نصي sh يحتوي على السطر

$PHP_COMMAND -r 'echo get_include_path();'

لا أستطيع تحرير هذا البرنامج النصي، ولكني بحاجة إلى أن يكون سطر الأوامر النهائي (مكافئًا لـ)

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

كيف يمكنني تحقيق ذلك؟


يوجد أدناه برنامج نصي يوضح المشكلة.

#!/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();'

البرنامج النصي متاح أيضًا عبر الإنترنت: http://Gist.github.com/276500

هل كانت مفيدة؟

المحلول

ويجب أن يكون هناك طريقة أسهل، ولكن الإصلاح واحد هو أن تحيط سطر الأوامر كامل مع التنصيص ثم وحدة التقييم ما يلي:

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

نصائح أخرى

وقراءة تعليقاتكم على إجابات أخرى، لقد حاولت أن فك ما المقصود حقا أن نسأل:

<اقتباس فقرة>   

ولدي النصي sh الذي يحتوي على سطر

$PHP_COMMAND -r 'echo get_include_path();'
     

وأنا لا يمكن تحرير هذا السيناريو، ولكن أنا في حاجة إلى سطر الأوامر في نهاية المطاف أن تكون (ما يعادل)

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

وكيف يمكنني تحقيق ذلك؟

إذا هذا يعكس بدقة الوضع الخاص بك، ثم وهذا هو اتجاه واحد:

وكتابة السيناريو آخر (أنا أسميها /some/path/to/bar.sh) لاستخدامها في PHP_COMMAND. نظرا لعدم ونقلت المتغير في السيناريو الأصلي، سيكون لديك للتأكد من أن اسم المسار الكامل من bar.sh ليس لديها أية أحرف قذيفة الخاصة (مثل المسافات).

/some/path/to/bar.sh

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

وبعد ذلك، لتشغيله، PHP_COMMAND مجموعة، وتشغيل البرنامج الأصلي (/path/to/foo.sh):

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

باش printf لديه بعض السحر السري الخاص الذي قد يساعد.يحاول:

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

أو بعض الاختلاف في ذلك.

يحرر:

أنا آسف، كان ينبغي لي أن أدرج بعض التوضيحات ومثالا.ال %q أسباب العلم printf لإضافة الهروب إلى السلسلة بحيث يمكن إعادة استخدامها في أمر آخر.مخرجات ذلك printf سيبدو هكذا (يتم الهروب من علامات الاقتباس المفردة):

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

يوضح هذا الأمر بعض عمليات الهروب الإضافية:

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

وماذا عن هذا؟

$PHP_COMMAND ' -r echo get_include_path();' 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top