1

I have the following code, thank-you to the people who helped me here

awk -F: '{if(system( "[ -d " $6 " ]") == 1) {print "The directory " $6 " does not exist for user " $1 }}' /etc/passwd

However, I was silly, and didn't explain my full problem. Rather than change the question on people, I figured I'd open a new question.

My goal is to output a list of users who have a home directory set in /etc/passwd that points to a folder that doesn't exist. (The above code successfully does this).

The problem I'm now running into is that I'd like to display "No Results" rather than a blank page, if every user does in fact have a home directory that exists.

So for example, if I run that code on a default RHEL system I get the following output:

# awk -F: '{if(system( "[ -d " $6 " ]") == 1) {print "The directory " $6 " does not exist for user " $1 }}' /etc/passwd
The directory /var/adm does not exist for user adm
The directory /var/spool/uucp does not exist for user uucp
The directory /var/gopher does not exist for user gopher
The directory /var/ftp does not exist for user ftp
The directory /var/empty/saslauth does not exist for user saslauth

The users listed above, really don't matter because they can't login, so I'd like to update my code to account for this like the following:

awk -F: '{if(system( "[ -d " $6 " ]") == 1 && $7 != "/sbin/nologin" ) {print "The directory " $6 " does not exist for user " $1 }}' /etc/passwd

This works as intended and no results are displayed, but I really want it to display "No Results", rather than not displaying anything. (This may seem like a silly requirement, but I'm going to be using this command in an automated tool which works a bunch better if there is output to look at).

I've googled and found a few examples of using NR, but haven't gotten it working right. I've also seen || and echo "No Results" on some types of commands, but can't seem to get that working either.

Google has not been helping me with other options, so I greatly appreciate your help!

3

You could

  • use a BEGIN block to set a flag
  • unset the flag when your current test finds a missing directory
  • check to see if the flag is still set in a END block and if so print your message

e.g.

awk -F: '
BEGIN{nores=1;} 
{if(system( "[ -d " $6 " ]") == 1 && $7 != "/sbin/nologin" ) {print "The directory " $6 " does not exist for user " $1; nores=0 }} 
END{if (nores) print "No results";}
' /etc/passwd
  • Thank-you. This is exactly what I wanted! I tried that general technique earlier, but I'm not familiar enough with AWK to have done it right! – Doug Apr 8 '15 at 23:48
  • @Doug - awk is your friend. It's almost as day to day useful as grep, find, and sed - and a lot easier to debug than any of those when things go wrong. A little time learning it will pay you back many times over. – Joe Apr 11 '15 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.