错误使用反引号执行?

发布于 2024-10-02 05:20:48 字数 650 浏览 0 评论 0原文

我尝试通过从文本文件读取命令来运行命令,但失败了。当我输入完全相同的行时,它正在工作,很难。我很惊讶它甚至尝试执行移动命令,但收到一条错误消息,翻译为“找不到文件或目录”。显然,这里的错误消息并没有说出真相。有人可以解释一下吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度℉ 2024-10-09 05:20:48

`head -n7 mkdoc|tail -n1` 被命令的输出替换,即 mv nutzer.1.gz ~/public_html/man/man1/。然后,此输出被解释为命令,即 mv 命令。

但它失败了,因为波形符扩展已经执行了。此时 ~ 尚未替换为您的主目录;它只是一个简单的波浪号字符。就好像您尝试执行

'mv' 'nutzer.1.gz' '~/public_html/man/man1/'

出于同样的原因,您不能使用 $HOME 或第二组反引号或任何其他动态构造。为此,您需要使用 eval,或将字符串传递给第二个 shell。

eval `head -n7 mkdoc|tail -n1`
bash -c "`head -n7 mkdoc|tail -n1`"

`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`"
☆獨立☆ 2024-10-09 05:20:48

波形符“~”不会插入反引号内。

[编辑]
相反,您应该能够使用: 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)"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文