Alex Variance
Alex Variance

Reputation: 39

Formatting output in linux

I am attempting questions from a textbook to brush up on my linux skills.

The question is :

  1. Using /etc/passwd, extract the user and home directory fields for all users on your Kali machine for which the shell is set to /bin/false. Make sure you use a Bash one-liner to print the output to the screen. The output should look similar to Listing 53 below:
kali@kali:~$ YOUR COMMAND HERE...
The user mysql home directory is /nonexistent
The user Debian-snmp home directory is /var/lib/snmp
The user speech-dispatcher home directory is /var/run/speech-dispatcher
The user Debian-gdm home directory is /var/lib/gdm3
Listing 53 - Home directories for users with /bin/false shells

This is my current progress:

┌──(root💀kali)-[/home/kali]
└─# cat /etc/passwd | cut -f 1 -d ":" | grep -v '/bin/false' | awk '{print "The user " $0;}' 

Output:

The user root
The user daemon
The user games
The user mail

I can't find a way to pipe/chain commands such that i can add "home directory" at the back to format my output. Can anyone help?

Upvotes: 0

Views: 763

Answers (1)

Barmar
Barmar

Reputation: 782785

You're discarding all the other fields of the file when you do cut -f 1 -d :.

There's no need to use cut, you can tell awk to use : as the field delimiter with the -F option.

You also don't need grep, since awk can do pattern matching. You also have that check backwards -- you're removing /bin/false instead of matching it.

awk -F: '$NF == "/bin/false" { printf("The user %s home directory is %s\n", $1, $6)}' /etc/passwd

Sample output:

The user bin home directory is /bin
The user daemon home directory is /sbin
The user adm home directory is /var/adm
The user lp home directory is /var/spool/lpd
The user news home directory is /var/spool/news
The user uucp home directory is /var/spool/uucp
The user portage home directory is /var/lib/portage/home
The user nobody home directory is /var/empty
The user systemd-journal-remote home directory is /dev/null
The user systemd-coredump home directory is /dev/null
The user systemd-network home directory is /dev/null
The user systemd-resolve home directory is /dev/null
The user systemd-timesync home directory is /dev/null
The user messagebus home directory is /dev/null
The user sshd home directory is /var/empty
The user polkitd home directory is /var/lib/polkit-1
The user u_app1 home directory is /srv/app/app1
The user nagios home directory is /dev/null
The user icinga home directory is /var/lib/icinga2
The user systemd-oom home directory is /dev/null

Upvotes: 1

Related Questions