错误使用反引号执行?
我尝试通过从文本文件读取命令来运行命令,但失败了。当我输入完全相同的行时,它正在工作,很难。我很惊讶它甚至尝试执行移动命令,但收到一条错误消息,翻译为“找不到文件或目录”。显然,这里的错误消息并没有说出真相。有人可以解释一下吗?
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
`head -n7 mkdoc|tail -n1`
被命令的输出替换,即mv nutzer.1.gz ~/public_html/man/man1/
。然后,此输出被解释为命令,即mv
命令。但它失败了,因为波形符扩展已经执行了。此时
~
尚未替换为您的主目录;它只是一个简单的波浪号字符。就好像您尝试执行出于同样的原因,您不能使用
$HOME
或第二组反引号或任何其他动态构造。为此,您需要使用eval
,或将字符串传递给第二个 shell。`head -n7 mkdoc|tail -n1`
is replaced by the output of the command, which ismv nutzer.1.gz ~/public_html/man/man1/
. This output is then interpreted as a command, amv
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 executeFor 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 useeval
, or pass the string to a second shell.波形符“~”不会插入反引号内。
[编辑]
相反,您应该能够使用: eval "$(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)"