हर कोई कहता है कि eval बुरा है, और आपको प्रतिस्थापन के रूप में $() का उपयोग करना चाहिए। लेकिन मैंने ऐसी स्थिति में भाग लिया है जहां unquoting $() के अंदर समान नहीं है।
पृष्ठभूमि यह है कि मुझे फ़ाइल पथों द्वारा अक्सर उन जगहों पर जला दिया गया है, और ऐसे सभी मार्गों को उद्धृत करना पसंद है। यह जानना चाहते हैं कि मेरे सभी एक्जिक्यूटिव कहां से आ रहे हैं। और भी भयावह, खुद पर भरोसा नहीं करते, और इसलिए बनाए गए आदेशों को प्रदर्शित करने में सक्षम होने की तरह मैं दौड़ने वाला हूं।
मैं नीचे बनाम $ (eval का उपयोग) में बदलाव की कोशिश, और आदेश नाम उद्धृत किया गया है कि क्या (यह cuz रिक्तियाँ सकता है)
BIN_LS="/bin/ls"
thefile="arf"
thecmd="\"${BIN_LS}\" -ld -- \"${thefile}\""
echo -e "\n Running command '${thecmd}'"
$($thecmd)
Running command '"/bin/ls" -ld -- "arf"'
./foo.sh: line 8: "/bin/ls": No such file or directory
echo -e "\n Eval'ing command '${thecmd}'"
eval $thecmd
Eval'ing command '"/bin/ls" -ld -- "arf"'
/bin/ls: cannot access arf: No such file or directory
thecmd="${BIN_LS} -ld -- \"${thefile}\""
echo -e "\n Running command '${thecmd}'"
$($thecmd)
Running command '/bin/ls -ld -- "arf"'
/bin/ls: cannot access "arf": No such file or directory
echo -e "\n Eval'ing command '${thecmd}'"
eval $thecmd
Eval'ing command '/bin/ls -ld -- "arf"'
/bin/ls: cannot access arf: No such file or directory
$("/bin/ls" -ld -- "${thefile}")
/bin/ls: cannot access arf: No such file or directory
तो ... यह भ्रामक है। $() निर्माण के अलावा एक उद्धृत कमांड पथ हर जगह मान्य है? एक छोटे, अधिक प्रत्यक्ष उदाहरण:
$ c="\"/bin/ls\" arf"
$ $($c)
-bash: "/bin/ls": No such file or directory
$ eval $c
/bin/ls: cannot access arf: No such file or directory
$ $("/bin/ls" arf)
/bin/ls: cannot access arf: No such file or directory
$ "/bin/ls" arf
/bin/ls: cannot access arf: No such file or directory
कैसे एक साधारण $($c)
मामले की व्याख्या करता है?
अनगिनत तारों का तुरंत मूल्यांकन किया जाता है। सी = ("/ बिन/एलएस"/बिन/एल *) वांछित से काफी अलग है। (echo "'$ {c [@]}'") के साथ देखें) c = ("/ bin/ls" "/ bin/l *") तो z = $ ("$ {c [@]}") विफल रहता है। तर्कों की एक सरणी का उपयोग $() के साथ अच्छी तरह से नहीं किया जा सकता है? – Shenme
हालांकि, सी = ("/ बिन/एलएस" "/ बिन/एल *") तो z = $ ($ {c [@]}) ठीक काम करता प्रतीत होता है। क्या यह निष्पादन योग्य रूप $() है जो आपको दिखाने के लिए था? – Shenme