2

I hope my question makes sense. I am quite new to this stuff.

I am using this piped command to make a list:

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

The output is this:

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

I need to put every line in this output to this command:

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

My output then looks like this:

-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

Please help me with this command.

Jamal
  • 763
  • 7
  • 22
  • 32
Retko
  • 332
  • 1
  • 5
  • 14
  • 2
    Grump: UUOC. `cut -d: -f7 /etc/passwd | sort -u` would suffice! And the answers so far are propagating the same mistakes. – Jonathan Leffler Oct 29 '13 at 22:58
  • 1
    In order to get the output specified by the OP, you'd need modify your command to: `cut -d: -f7 /etc/passwd | sort -u | xargs ls -l` As it is, your command only outputs `/bin/bash`, not `-rwxr-xr-x. 1 root root 917576 11. bře 2013 /bin/bash ` as requested by the OP. – delliottg Oct 30 '13 at 19:27

4 Answers4

2

xargs is ideal for this kind of command-line work. It executes the command given as argument repeatedly over the lines of input. Pipe your output into xargs ls -l:

cat /etc/passwd | cut -d: -f7 | sort -n | uniq | xargs ls -l
salezica
  • 74,081
  • 25
  • 105
  • 166
0

Another method:

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

Edited again to add xargs at the end (which I'd spaced out earlier), now it gives the proper output, but it's essentially uʍop ǝpısdn's answer re-worked.

@uʍop ǝpısdn's answer works well.

delliottg
  • 3,950
  • 3
  • 38
  • 52
  • How does this differ materially from `ls -l $(cut -d: -f7 /etc/passwd | sort -u)`? What is the benefit of executing 3 extra commands? – Jonathan Leffler Oct 29 '13 at 22:59
  • For brevity, none, for a OP who's learning bash syntax, maybe they learn that there's always more than one way to do something. This is evidenced in the variety of methods presented by others that all result in the same output. – delliottg Oct 30 '13 at 16:29
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

But if you just need to get some informations about this files (like permissions for example), prefer the stat command in this case. See man stat for more details. See this answer to know how to read a command output in a loop.

NB : sort -u already filter duplicates ;)

Community
  • 1
  • 1
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
0

Just for fun:

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[@]}"

Why? because I'm using bash's hash capabilities for the unique part, and then ls is smart enough to do the sorting for me.

And just for fun (again):

ls -l $( (IFS=$':\n'; printf '%.0s%.0s%0.s%0.s%0.s%.0s%s\n' $(</etc/passwd) ) | sort -u)
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104