wrong use of backticks execution?
문제
I tried to run a command by reading it from a textfile, but it failed. when i enter the exact same line it is working, tough. im suprised that it did even try to execute the move command, but got an errormessage that translates to "File or directory not found". obviously the errormessage is not telling the truth here. can someone explain that?
s39339@compute:~/spr/man/de$ head -n7 mkdoc|tail -n1
mv nutzer.1.gz ~/public_html/man/man1/
s39339@compute:~/spr/man/de$ `head -n7 mkdoc|tail -n1`
mv: Verschieben von „nutzer.1.gz“ nach „~/public_html/man/man1/“ nicht möglich: Datei oder Verzeichnis nicht gefunden
s39339@compute:~/spr/man/de$ ls
gzip mkdoc nutzer.1 nutzer.1.gz nutzer.pod rbsh
s39339@compute:~/spr/man/de$ mv nutzer.1.gz ~/public_html/man/man1/
s39339@compute:~/spr/man/de$
I am doin this for school so an answer would be nice. the way we get to our results doesn't matter, although what i tried seems way unnessecary.
해결책
`head -n7 mkdoc|tail -n1`
is replaced by the output of the command, which is mv nutzer.1.gz ~/public_html/man/man1/
. This output is then interpreted as a command, a mv
command.
It fails, though, because tilde expansion has already been performed. ~
is not substituted with your home directory at this point; it's just a plain tilde character. It's as if you had tried to execute
'mv' 'nutzer.1.gz' '~/public_html/man/man1/'
For the same reason you cannot use $HOME
, or a second set of backticks, or any other dynamic construct. To do that you will need to use eval
, or pass the string to a second shell.
eval `head -n7 mkdoc|tail -n1`
bash -c "`head -n7 mkdoc|tail -n1`"
다른 팁
The tilde "~" is not interpolated inside backticks.
[edited] Instead you should be able to use: eval "$(head -n7 mkdoc|tail -n1)"