bash awk directory – How to Check if a File Exists in Awk

awkbashdirectory

I'm trying to generate a list of users who have a home directory set which does not exist. It seems I should be able to do this with awk, but something is wrong with my syntax.

It keeps telling me "Invalid Syntax" at the ]. What am I doing wrong?

awk -F: '{ if(![ -d "$6"]){ print $1 " " $3 " " $7}}' /etc/passwd

The final code I'm probably going to end up using is:

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

And I have a related question here.

Best Answer

You could use

system(command)
    Execute the operating-system command command and then
    return to the awk program. Return command’s exit status. 

e.g.:

awk -F: '{if(system("[ ! -d " $6 " ]") == 0) {print $1 " " $3 " " $7}}' /etc/passwd
Related Question