gpt4 book ai didi

Linux 命令 : Why does the redirection operator - | i. e。管道在这里失败?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:56 24 4
gpt4 key购买 nike

我正在学习 Shell (Bash) 脚本的初级读物并有以下疑问:

  • Why does not the following command print the contents of cp's directory : which cp | ls -l

  • Does not piping by definition mean that we pass the output of one command to another i.e. redirect the output ?

有人可以帮帮我吗?我是新手..

最佳答案

which 的输出被管道传输到 ls 的标准输入。但是,ls 不接受任何标准输入。您希望它(我想)作为参数传递。有几种方法可以做到这一点:

which cp | xargs ls -l

ls -l `which cp`

ls -l $(which cp)

在第一个示例中,xargs 命令采用前一个命令的标准输出,并将每一行作为名称紧跟在 xargs 之后的命令的参数。所以,例如

find / | xargs ls -l

将对文件系统中的每个文件执行 ls -l(对于特殊命名的文件存在一些问题,但这超出了本答案的范围)。

其余两个大致相同,并使用 shell 执行此操作,将 which 的输出扩展到 cp 的命令行。

关于Linux 命令 : Why does the redirection operator - | i. e。管道在这里失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29692233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com