Jeder sagt Eval ist böse, und Sie sollten $() als Ersatz verwenden. Aber ich bin in eine Situation geraten, in der die nicht quitting nicht innerhalb $() behandelt wird.
Hintergrund ist, dass ich zu oft von Dateipfaden mit Leerzeichen in ihnen verbrannt worden bin, und so zitieren alle solche Pfade. Mehr Paranoia darüber, dass ich wissen möchte, woher alle meine ausführbaren Dateien kommen. Noch paranoider, ich vertraue mir selbst nicht und bin so in der Lage, die erstellten Befehle anzuzeigen, die ich gerade ausführen werde.
Im Folgenden werde ich versuchen Variationen vs. $ eval(), und ob der Befehl Namen zitiert wird (Cuz es könnte Leerzeichen enthalten)
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
So ... das ist verwirrend. Ein in Anführungszeichen gesetzter Befehlspfad ist überall gültig, außer in einem Konstrukt $()? Ein kürzeres, direkteres Beispiel:
$ 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
Wie kann man den einfachen $($c)
Fall erklären?
Unquoted Strings werden sofort ausgewertet. c = ("/ bin/ls"/bin/l *) macht etwas ganz anderes als gewünscht. (schau mit echo "'$ {c [@]}'") c = ("/ bin/ls" "/ bin/l *") dann scheitert z = $ ("$ {c [@]}"). Ein Array von Argumenten kann nicht gut mit $() verwendet werden? – Shenme
Allerdings scheint c = ("/ bin/ls" "/ bin/l *") dann z = $ ($ {c [@]}) gut zu funktionieren. Ist das die ausführbare Form $(), die du mir zeigen wolltest? – Shenme