Computing.Net > Forums > Unix > Korn Shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Korn Shell script

Reply to Message Icon

Name: Bridge
Date: August 21, 2002 at 15:34:18 Pacific
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Frank
Date: August 21, 2002 at 23:49:01 Pacific
Reply:

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



0

Response Number 2
Name: Frank
Date: August 22, 2002 at 08:58:55 Pacific
Reply:

Hey this is the first script which is jerry compliant.

Ha, :-))

I have fun
Frank


0

Response Number 3
Name: Jerry Lemieux
Date: August 22, 2002 at 11:49:59 Pacific
Reply:

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


0

Response Number 4
Name: Ned
Date: August 22, 2002 at 12:47:42 Pacific
Reply:

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 problem

Below can be the solution:


#!/bin/ksh

while 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


0

Response Number 5
Name: Ned
Date: August 23, 2002 at 12:57:44 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Ned
Date: August 23, 2002 at 14:27:56 Pacific
Reply:

Sorry for the unsetting IFS in Response Number 5 above.

Please insert the line:
unset IFS

Above the line:
set -A fields `ls -dl $home`

Thanks,


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Korn Shell script

Korn shell script doubt ??? www.computing.net/answers/unix/korn-shell-script-doubt-/3407.html

sftp Korn shell script www.computing.net/answers/unix/sftp-korn-shell-script/4741.html

Korn Shell script for julian date? www.computing.net/answers/unix/korn-shell-script-for-julian-date/3729.html