How do you check if a file exists within awk? [-d 'filename'] failing
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
I don't think that [ -d ]
is an awk
thing, that's a shell thing. I would just do it this way instead:
awk -F: '{ print $1,$3,$7}' /etc/passwd |
while read name uid shell; do
[ -d "/home/$name" ] || echo "$name $uid $shell";
done
Of course, as very correctly pointed out by @Janis, you can do the whole thing in the shell:
while IFS=: read name x uid x x x shell rest; do
[ -d "/home/$name" ] || echo "$name $uid $shell"
done < /etc/passwd
You can use getline:
awk 'BEGIN {print getline < "file" < 0 ? "not exists" : "exists"}'