Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Appreciate if can give help in writing Korn Shells to resolve the following problems:
To check that all users in the
/etc/passwd file have a home directory and that the directory is owned by the correct
user and group. Also check that the directory has permissions rwxr-sr-x. Generate a report of any abnormalities

Hi Bridge,
here is the beginning:
IFS=:
cat /etc/passwd |while read user a a a a home rest
do
if [ `find $home -user $user -type d -perm 2755` ]
then
echo Wahr $user $home
else
echo falsch $user $home
fi
done
IFS=You can format the Wahr and falsch lines
as you would like to have it in your report.No RISK fun
Frank

Wrong. You have a useless use of the cat command (See UUOC). Get rid of the cat command, enclose the code in { } and use < /etc/passwd. If you use cat on the comp.unix.shell newsgroup, you will be bombarded with UUOC awards. Your script should be:
{
IFS=:
while read user a a a a home rest
do
if [ `find $home -user $user -type d -perm 2755` ]
then
echo Wahr $user $home
else
echo falsch $user $home
fi
done
IFS=} < /etc/passwd
This keeps you from getting the UUOC award and eliminates an uneeded pipe.
Another thing I'd recommend is not checking system users. On Solaris users sys, root, nobody and others have / as a home directory. You may not want to be making the permissions on system user owned directories SGID. Also, find from / takes a little while to execute. Perhaps it would be better to test for the home directory, if it exists do a ls -lad on /home/jlemieux and read the permissions, owner, group from the ls output and create your report. Might be a little more efficient than the find command. However, other than using the cat command where it is not needed, there is nothing wrong with your solution. Even with the cat command, it is fine. Just remember the UUOC award is lurking out there somewhere waiting to be awarded.
Jerry

As per my understanding following are wrong with above:
1. find won't work in this way as action is not specified
2. find will look for the subdirectories inside the $home directories meeting the criteria
3. find is not checking for the group
4. Not specific of the problemBelow can be the solution:
#!/bin/kshwhile read rec
do
user=`echo $rec | awk -F: '{ print $1 }'`
group=`echo $rec | awk -F: '{ print $4 }'`
home=`echo $rec | awk -F: '{ print $6 }'`
agroup=`grep ":$group:" /etc/group | awk -F: '{ print $1 }`
if [ -d "$home" ]
then
set -A fields `ls -dl $home`
if [ "${fields[0]}" = "drwxr-sr-x" ]
then
if [ "${fields[2]}" = "$user" ]
then
if [ "${fields[3]}" = "$agroup" ]
then
echo "FOUND OK\t= $user $home"
else
echo "Wrong Group\t= $user $home"
fi
else
echo "Wrong User\t= $user $home"
fi
else
echo "Wrong Perm\t= $user $home"
fi
else
echo "No Directory\t= $user $home"
fi
done < /etc/passwd
# Format report as per requirement

I have got some more time which I can spare here for the learners of the scripting.
Below is, how we can avoid awk used in my given solution above.
#!/bin/ksh
IFS=:
while read user a1 a2 group a3 home etc
do
agroup=`grep ":$group:" /etc/group | cut -d: -f1`
if [ -d "$home" ]
then
set -A fields `ls -dl $home`
if [ "${fields[0]}" = "drwxr-sr-x" ]
then
if [ "${fields[2]}" = "$user" ]
then
if [ "${fields[3]}" = "$agroup" ]
then
echo "FOUND OK\t= $user $home"
else
echo "Wrong Group\t= $user $home"
fi
else
echo "Wrong User\t= $user $home"
fi
else
echo "Wrong Perm\t= $user $home"
fi
else
echo "No Directory\t= $user $home"
fi
done < /etc/passwd

Sorry for the unsetting IFS in Response Number 5 above.
Please insert the line:
unset IFSAbove the line:
set -A fields `ls -dl $home`Thanks,

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |