在unix系统中组合两个命令的输出

4d4*_*d4c 12 unix shell scripting command

我需要结合两个命令的输出.

例如:

如果我输入ls -l && file *它会给我

-rw-rw-r-- 1 user user 1356 2012-01-21 07:45 string.c
-rwxrwxr-x 1 user user 7298 2012-01-21 07:32 string_out
-rw-rw-r-- 1 user user  777 2012-01-18 21:44 test

string.c:   ASCII C program text, with CRLF line terminators
string_out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
test:       POSIX shell script text executable
Run Code Online (Sandbox Code Playgroud)

但我想要的是:

-rw-rw-r-- 1 user user 1356 2012-01-21 07:45 string.c string.c:   ASCII C program text, with CRLF line terminators
-rwxrwxr-x 1 user user 7298 2012-01-21 07:32 string_out string_out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
-rw-rw-r-- 1 user user  777 2012-01-18 21:44 test test:       POSIX shell script text executable
Run Code Online (Sandbox Code Playgroud)

有什么建议怎么做?

Kyl*_*ton 7

Shiplu有一个很好的简单解决方案,在bash中你可以不用变量就可以做到:

for x in *; do 
  echo "$(ls -dl $x) $(file $x)"
done;
Run Code Online (Sandbox Code Playgroud)

要么:

for x in *; do echo "$(ls -dl $x) $(file $x)"; done;
Run Code Online (Sandbox Code Playgroud)

在bash中,$(cmd)获取输出cmd并将其放在命令行上,这对于这样的情况非常有用.

的$()形式可以是比使用反引号(`cmd`),因为它安全地嵌套容易发生误差小:

echo $(ls -l $(which bash))
Run Code Online (Sandbox Code Playgroud)

使用反引号,你必须多次逃避报价等事情


gle*_*man 5

paste是你的朋友.使用bash进程替换:

paste <(ls -l | sed 1d) <(file *)
Run Code Online (Sandbox Code Playgroud)

编辑:添加sed命令删除ls输出的第一行("total:xx")