2011-02-16 101 views
2
$ which file 
/usr/bin/file 
$ file /usr/bin/file 
/usr/bin/file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped 

爲什麼這不起作用?如何將Linux的哪個命令輸出到Linux文件命令中?

$ which file | file 

Usage: file [-bcikLhnNrsvz0] [-e test] [-f namefile] [-F separator] [-m magicfiles] file... 
    file -C -m magicfiles 

嘗試file --help瞭解更多信息。

+0

可能重複的[爲什麼我的Bash計數器在while循環後重置](http://stackoverflow.com/questions/5006229/why-does-my-bash-counter-reset-after-while-loop) – ajreal 2011-02-16 14:06:25

回答

5

file預計在命令行上的參數,而不是它的標準輸入,除非你告訴它,否則:

$ which file | file -f - 

或者:

$ file `which file` 

或者,完整性:

$ which file | xargs file 
+1

$哪個文件| xargs文件 爲完整性添加選擇此答案,因爲它具有最短的管道選項。 – nelaaro 2011-02-18 08:44:44

6

您可能正在尋找xargs或shell擴展。嘗試:

$ which file | xargs file 

$ file `which file` 
+0

感謝您使用您的選項來完成上述答案 – nelaaro 2011-02-18 08:48:45

4

嘗試後引號代替,例如

$ file `which python` 
/usr/bin/python: symbolic link to `python2.6' 

您的示例不起作用,因爲您的管道將which命令的輸出發送到file命令的stdin。但是文件在stdin上不起作用 - 它在命令行參數上起作用。