关于Linux命令的组成

发布于 2024-08-19 07:43:23 字数 505 浏览 2 评论 0原文

假设:

  1. 文件f的路径为~/f
  2. “which f”显示“~/f”,

那么,

which f | cat 显示~/f。所以这里的cat应用于~/f的引用,与cat ~/f不同。

我的问题是:如何使用由 whichcat 组成的一个命令来实现 cat ~/f 的结果?当我事先不知道which f的结果时,使用这种组合会非常方便。目前,如果我事先不知道 which f 的结果,我必须先调用 which f,然后复制粘贴结果以提供 less

一个相关的问题是:如何将 which f 的结果分配给变量?

多谢!

Assuming:

  1. the path of file f is ~/f
  2. "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 技术交流群。

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

发布评论

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

评论(7

极度宠爱 2024-08-26 07:43:23

尝试:

cat `which ~/f`

对于相关问题:

foo=`which ~/f`
echo $foo

Try:

cat `which ~/f`

For the related question:

foo=`which ~/f`
echo $foo
娇柔作态 2024-08-26 07:43:23

猫“`which f`”

cat "`which f`"

°如果伤别离去 2024-08-26 07:43:23

就像 bash 中这样:

cat "$(which f)"
var="$(which f)"

Like so in bash:

cat "$(which f)"
var="$(which f)"
夏见 2024-08-26 07:43:23

你想要的是:

cat `which f`

What you want is:

cat `which f`
去了角落 2024-08-26 07:43:23

在其中 f | cat cat 程序在标准输入上获取 which f 的输出。然后,它只是传递标准输入,因此结果与普通的 which f 相同。在调用cat ~/f 中,数据作为参数传递给命令。 cat 然后打开文件 ~/f 并显示其内容。

要获取 which f 的输出作为 cat 的参数,您可以像其他人回答的那样,使用反引号或 $()

cat `which f`
cat $(which f)

这里shell 获取 which f 的输出并将其作为 cat 的参数插入。

In which f | cat the cat program gets the output of which f on standard input. That then just passes that standard input through, so the result is the same as a plain which f. In the call cat ~/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 to cat you can, as others have answered, use backticks or $():

cat `which f`
cat $(which f)

Here the shell takes the output of which f and inserts it as a parameter for cat.

抚笙 2024-08-26 07:43:23

bash 中,您可以使用:

cat "$(which f)"

输出 which 找到的 f 的内容。与反引号解决方案一样,它获取 $(...) 中命令的输出,并将其用作 cat 命令的参数。

我更喜欢 $(...) 而不是反引号方法,因为前者可以嵌套在更复杂的情况下。

which 的输出分配给变量的方法类似:

full_f="$(which f)"

在这两种情况下,最好在 case f 中使用引号,或者它的路径包含空格,就像令人发指的那样犯罪是:-)

当我想编辑给定子目录下一组具有相似名称的文件时,我经常使用类似的技巧:

vim $(find . -type f -name Makefile)

这将为我提供一个 vim 会话所有 makefile(显然,如果数量很大,我会使用 sedperl 来集体修改它们,而不是 vim )。

In bash, you can use:

cat "$(which f)"

to output the contents of the f that which finds. This, like the backtick solution, takes the output of the command within $(...) and uses that as a parameter to the cat 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:

full_f="$(which f)"

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:

vim $(find . -type f -name Makefile)

which will give me a single vim session for all the makefiles (obviously, if there were a large number, I'd be using sed or perl to modify them en masse instead of vim).

随梦而飞# 2024-08-26 07:43:23

cat 将文件内容回显到标准输出。当你写东西时 | cat,cat 所处理的文件是标准输入,它连接到 stuff 的输出(因为管道是文件,就像 unix 中的几乎所有其他东西一样)。

没有像 Lisp 程序员使用这个词那样的意义上的引用。

cat echos the contents of files to the standard output. When you write stuff | cat, the file cat works on is the standard input, which is connected to the output of stuff (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.

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