Issue
The below mentioned code is not working properly (CentOS 7/bash file code):
#!/bin/bash
cat /etc/passwd | egrep -v '^(root|halt|sync|shutdown)' |
awk -F: '($7 != "/sbin/nologin" && $7 != "/bin/false") { print $1 " " $6 }' |
while read user dir; do
if [ ! -d "$dir" ]; then
echo "The home directory ($dir) of user $user does not exist."
fi
done
The code written here is going in infinite loop. Can you please tell me what to do?
Solution
As has already been suggested, there is an unnecessary use of cat into grep and then an unnecessary pipe of grep into awk. You can condense the execution with the following:
#!/bin/bash
awk -F: '$1 !~ /root|halt|sync|shutdown/ && $7 != "/sbin/nologin" && $7 != "/bin/false"
{ print $1 " " $6 }' /etc/passwd | while read user dir;
do
if [[ ! -d "$dir" ]];
then
echo "The home directory ($dir) of user $user does not exist."
fi
done
Answered By - Raman Sailopal Answer Checked By - Pedro (WPSolving Volunteer)