Unix pipe into ls

好久不见. 提交于 2019-11-28 17:28:33
fedorqui

To do that you need xargs:

which studio | xargs ls -l

From man xargs:

xargs - build and execute command lines from standard input

To fully understand how pipes work, you can read What is a simple explanation for how pipes work in BASH?:

A Unix pipe connects the STDOUT (standard output) file descriptor of the first process to the STDIN (standard input) of the second. What happens then is that when the first process writes to its STDOUT, that output can be immediately read (from STDIN) by the second process.

ls does not read its arguments from standard input, but from the command line. To get the directory at the command line, you have to use command substitution:

ls -l "$( which studio )"

(The double quotes are needed if the path might contain whitespace.)

Since ls -l does not take any input, it does not do anything regarding the output of which studio. The important thing here is understanding the difference between standard input and arguments. The standard input is a special file that is read using the scanf procedure (by a program in C for example), and the arguments to a program are passed to the main procedure as the argv and argc parameters. argv is an array of null terminated arrays of char, and argc is the length of that array.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!