使用cat命令输出中的每一行作为(ls -l文件)的文件

时间:2013-10-29 22:36:31

标签: bash shell sh

我希望我的问题有道理。我对这些东西很陌生。

我正在使用此管道命令制作一个列表:

cat /etc/passwd | cut -d: -f7 |sort -n  |uniq  

输出是这样的:

/bin/bash   
/bin/false   
/bin/sync   
/no/shell  
/sbin/halt 
/sbin/nologin  
/sbin/shutdown 
/usr/sbin/nologin

我需要将此输出中的每一行都放到此命令中:

ls -l /bin/bash
ls -l /bin/false 
ls -l /bin/sync

我的输出看起来像这样:

-rwxr-xr-x. 1 root root 917576 11. bře  2013 /bin/bash   
-rwxr-xr-x. 1 root root  29920  8. kvě 09.45 /bin/false
-rwxr-xr-x. 1 root root  29940  8. kvě 09.45 /bin/sync

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:2)

xargs非常适合这种命令行工作。 在输入行上重复执行参数的命令。将输出管道输入xargs ls -l

cat /etc/passwd | cut -d: -f7 | sort -n | uniq | xargs ls -l

答案 1 :(得分:0)

另一种方法:

echo ls -l `cat /etc/passwd | cut -d: -f7 | sort -n | uniq` | xargs

再次编辑在末尾添加xargs(我之前已经分开了),现在它提供了正确的输出,但它基本上是uʍopǝpısdn的答案重新开始。

@uʍopǝpısdn的答案很有效。

答案 2 :(得分:0)

while read; do ls -l $REPLY; done <<< "$(cat /etc/passwd|cut -d: -f7|sort -u)"
# or
cat /etc/passwd|cut -d: -f7|sort -u|while read; do ls -l $REPLY; done

但是如果您只需要获取有关此文件的一些信息(例如权限),则在这种情况下更喜欢stat命令。有关详细信息,请参阅man stat。请参阅此answer以了解如何在循环中读取命令输出。

注意:sort -u已经过滤重复项;)

答案 3 :(得分:0)

只是为了好玩:

declare -A shell_you_re_nuts
while IFS=: read -r -a strawberry_fields; do
    shell_you_re_nuts[${strawberry_fields[6]}]=1
done < /etc/passwd
ls -l "${!shell_you_re_nuts[@]}"

为什么呢?因为我正在使用bash的哈希功能来处理这个独特的部分,然后ls足够聪明,可以为我做排序。

只是为了好玩(再次):

ls -l $( (IFS=$':\n'; printf '%.0s%.0s%0.s%0.s%0.s%.0s%s\n' $(</etc/passwd) ) | sort -u)