相关疑难解决方法(0)

为什么我不能将一个命令的路径名输出重定向到“cd”?

我试图cd接受从另一个命令重定向到它的目录名称。这些方法都不起作用:

$ echo $HOME | cd
$ echo $HOME | xargs cd
Run Code Online (Sandbox Code Playgroud)

这确实有效:

$ cd $(echo $HOME)
Run Code Online (Sandbox Code Playgroud)

为什么第一组命令不起作用,还有其他命令也会以这种方式失败吗?

shell pipe io-redirection cd-command

29
推荐指数
2
解决办法
6454
查看次数

为什么有些命令不从其标准输入中读取?

我想知道什么时候应该使用管道,什么时候不应该使用。

例如,要杀死某些处理 pdf 文件的进程,使用管道将无法执行以下操作:

ps aux | grep pdf | awk '{print $2}'|kill
Run Code Online (Sandbox Code Playgroud)

相反,我们只能通过以下方式做到这一点:

kill $(ps aux| grep pdf| awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)

或者

ps aux | grep pdf | awk '{print $2}'| xargs kill
Run Code Online (Sandbox Code Playgroud)

根据man bash(版本4.1.2):

The standard output of command is connected via a pipe to the standard input of command2.
Run Code Online (Sandbox Code Playgroud)

对于上述场景:

  • 的标准输入grep是标准输出ps。那个有效。
  • 的标准输入awk是标准输出grep。那个有效。
  • 的标准输入kill是标准输出awk。那行不通。

以下命令的标准输入总是从前一个命令的标准输出获得输入。

  • 为什么它不能与killor 一起使用rm
  • 什么是之间的不同 …

shell pipe stdin arguments

23
推荐指数
3
解决办法
9723
查看次数

标签 统计

pipe ×2

shell ×2

arguments ×1

cd-command ×1

io-redirection ×1

stdin ×1