---------- Post updated 15-01-10 at 12:08 AM ---------- Previous update was 14-01-10 at 04:54 PM ----------
#! /bin/bash
infoUsers=$(cat /etc/passwd | cut -d':' -f1,3,6)
for infoUser in $infoUsers
do
userName=$(echo $infoUser | cut -d':' -f1)
uid=$(echo $infoUser | cut -d':' -f2)
uDir=$(echo $infoUser | cut -d':' -f3)
idGroup=$(cat /etc/passwd | cut -d':' -f4)
if [ '"$uid" -ge "500" && ! $uDir -eq "/dev/null"' ];
then
if [ ! -d $uDir ]
then
$(mkdir $uDir)
fi
$(chown $userName $uDir)
groupName=$(cat /etc/group | grep $idGroup | cut -d':' -f1)
$(chgrp $groupName $uDir)
$(chmod o-rwx $uDir)
if [ ! -f "$uDir/www/index.html" ]
then
$(echo "Welcome!" > "$uDir""/www/index.html")
$(chmod o-w+rx "$uDir""/www/index.html")
fi
fi
done
for file in /root/logins/login*
do
echo "$file" >> "/root/logins/check.log"
cat "$file" >> "/root/logins/check.log"
done
well this is what a got, but when I was going to test it this script bust up my users or something and my linux got **** up. What you think was the problem
What happened to your system? Can you describe any effects, or post any output?
Where corrections were applied:
Old code is in red
New code is in Green
Comments are in Blue
#! /bin/bash
infoUsers=$(cat /etc/passwd | cut -d':' -f1,3,6)
# If you want the group, too, then you'll have to get it here, not inside the loop
infoUsers=$(cat /etc/passwd | cut -d':' -f1,3,6,4)
for infoUser in $infoUsers
do
userName=$(echo $infoUser | cut -d':' -f1)
uid=$(echo $infoUser | cut -d':' -f2)
uDir=$(echo $infoUser | cut -d':' -f3)
idGroup=$(cat /etc/passwd | cut -d':' -f4)
# See above comment. Better to get the group outside the loop, not inside
idGroup=$(echo $infoUser | cut -d':' -f4)
if [ '"$uid" -ge "500" && ! $uDir -eq "/dev/null"' ];
# As soon as you surround a variable or constant with quotes it becomes a
string, which isn't suitable for testing with -gt/-lt
# Also, you don't have to quote the whole test expression
if [ $uid -ge 500 && ! "$uDir" -eq "/dev/null" ];
then
if [ ! -d $uDir ]
then
$(mkdir $uDir)
# No need to capture the output of mkdir here using $()
mkdir $uDir
fi
$(chown $userName $uDir)
#Again, no need to capture
chown $userName $uDir
# This is optional. You can change the ownerships by ID just as well
groupName=$(cat /etc/group | grep $idGroup | cut -d':' -f1)
$(chgrp $groupName $uDir)
$(chmod o-rwx $uDir)
#Again, no need to capture
chgrp $groupName $uDir
chmod o-rwx $uDir
if [ ! -f "$uDir/www/index.html" ]
then
$(echo "Welcome!" > "$uDir""/www/index.html")
$(chmod o-w+rx "$uDir""/www/index.html")
fi
fi
done
for file in /root/logins/login*
do
echo "$file" >> "/root/logins/check.log"
cat "$file" >> "/root/logins/check.log"
done
well my sistem got all the test replaced by xxxxxx and [][][][][][][] then I restarted it but it will not boot now so I installed a new one, i'm running this on a virtual machine. Thanks for every thing