关于Linux命令的组成
假设:
- 文件f的路径为~/f
- “which f”显示“~/f”,
那么,
which f | cat
显示~/f
。所以这里的cat
应用于~/f
的引用,与cat ~/f
不同。
我的问题是:如何使用由 which
和 cat
组成的一个命令来实现 cat ~/f
的结果?当我事先不知道which f
的结果时,使用这种组合会非常方便。目前,如果我事先不知道 which f
的结果,我必须先调用 which f
,然后复制粘贴结果以提供 less
。
一个相关的问题是:如何将 which f
的结果分配给变量?
多谢!
Assuming:
- the path of file f is ~/f
- "which f" shows "~/f",
Then,
which f | cat
shows ~/f
. So cat
here is applied to the quotation of ~/f
, which is different with cat ~/f
.
My question is: how I could use one command composed of which
and cat
to achieve the result of cat ~/f
? When I don't know the result of which f
in advance, using this composition can be very convenient. Currently, if I don't know the result of which f
in advance, I have to invoke which f
first, and copy-paste the result to feed less
.
A related question is: how can I assign the result of which f
to a variable?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试:
对于相关问题:
Try:
For the related question:
猫“`which f`”
cat "`which f`"
就像
bash
中这样:Like so in
bash
:你想要的是:
What you want is:
在其中 f | cat
cat
程序在标准输入上获取which f
的输出。然后,它只是传递标准输入,因此结果与普通的which f
相同。在调用cat ~/f
中,数据作为参数传递给命令。cat
然后打开文件~/f
并显示其内容。要获取
which f
的输出作为cat
的参数,您可以像其他人回答的那样,使用反引号或$()
:这里shell 获取
which f
的输出并将其作为cat
的参数插入。In
which f | cat
thecat
program gets the output ofwhich f
on standard input. That then just passes that standard input through, so the result is the same as a plainwhich f
. In the callcat ~/f
the data is passed as a parameter to the command.cat
then opens the file~/f
and displays it's contents.To get the output of
which f
as a parameter tocat
you can, as others have answered, use backticks or$()
:Here the shell takes the output of
which f
and inserts it as a parameter forcat
.在
bash
中,您可以使用:输出
which
找到的f
的内容。与反引号解决方案一样,它获取$(...)
中命令的输出,并将其用作cat
命令的参数。我更喜欢
$(...)
而不是反引号方法,因为前者可以嵌套在更复杂的情况下。将
which
的输出分配给变量的方法类似:在这两种情况下,最好在 case
f
中使用引号,或者它的路径包含空格,就像令人发指的那样犯罪是:-)当我想编辑给定子目录下一组具有相似名称的文件时,我经常使用类似的技巧:
这将为我提供一个
vim
会话所有 makefile(显然,如果数量很大,我会使用sed
或perl
来集体修改它们,而不是vim
)。In
bash
, you can use:to output the contents of the
f
thatwhich
finds. This, like the backtick solution, takes the output of the command within$(...)
and uses that as a parameter to thecat
command.I prefer the
$(...)
to the backtick method since the former can be nested in more complex situations.Assigning the output of
which
to a variable is done similarly:In both cases, it's better to use the quotes in case
f
, or it's path, contains spaces, as heinous as that crime is :-)I've often used a similar trick when I want to edit a small group of files with similar names under a given sub-directory:
which will give me a single
vim
session for all the makefiles (obviously, if there were a large number, I'd be usingsed
orperl
to modify them en masse instead ofvim
).cat
将文件内容回显到标准输出。当你写东西时 | cat,cat 所处理的文件是标准输入,它连接到stuff
的输出(因为管道是文件,就像 unix 中的几乎所有其他东西一样)。没有像 Lisp 程序员使用这个词那样的意义上的引用。
cat
echos the contents of files to the standard output. When you writestuff | cat
, the file cat works on is the standard input, which is connected to the output ofstuff
(because pipes are files, just like nearly everything else in unix).There is no quoting going on in the sense that a lisp programmer would use the word.