ksh and while loops
We are setting up a new server and I have to add accounts from our old server, 2000 - 3000, of them. I am trying to set up an adduser script which will read userdata from a file and then add there account.
I have set up a test server which is running Fedora 4 Core, the new server will be running this also.
The script I am using works fine on Tru64-Unix and I have messaged to, I hope so it will work on Redhat. The problem is it appears that in the:
while read -u3 line, line it isn't reading it. If I put the read -u3 line by itself and then echo $line I get the output of the file line it reads. put it back into the while statement and nothing appears to happen. No account added no errors meesages nothing.
I'm sure it has to be something simple I am doing but beats me if I can figure it out.
Here is the script and thanks for any help you can give.
#!/bin/ksh -ph
PATH=/sbin:/usr/sbin:/usr/bin:/tcb/bin
export PATH
umask 022
if [ `id -u` -ne 0 ]
then
echo 'Please su to root first.\n'
exit 1
fi
if test ! -x /usr/sbin/useradd
then
echo 'Error, /usr/sbin/useradd missing or not executable.\n'
exit 1
fi
trap 'exit 1' 1 2
#SET FILE NAME
exec 3< ./userdata
#add new line to the userlist
echo 'password, account, email' >> ./userlist
#####################################################
#HERE WE ARE GOING TO READ FROM A FILE - read DATA
# the file looks like this:
# FIRSTNAME LASTNAME USERNAME USERIDNUM
# $1 = FIRSTNAME
# $2 = LASTNAME
# $3 = USERNAME
# $4 = USERIDNUM
#####################################################
while read -u3 DATA
do
print doing
set $DATA
NAME=$1' '$2
FNAME=$1
LNAME=$2
USERNAME=$3
UID=$4
LOGGROUP=student
LSHELL=/bin/sh
SKEL=/usr/skel
echo $NAME
echo $FNAME
case "$USERNAME" in
[AaBb]*)
PARENT=/usr/users/student.A-B
;;
[CcDd]*)
PARENT=/usr/users/student.C-D
;;
[EeFfGg]*)
PARENT=/usr/users/student.E-G
;;
[HhIiJj]*)
PARENT=/usr/users/student.H-J
;;
[KkLl]*)
PARENT=/usr/users/student.K-L
;;
[MmNnOo]*)
PARENT=/usr/users/student.M-O
;;
[PpQqRr]*)
PARENT=/usr/users/student.P-R
;;
[SsTt]*)
PARENT=/usr/users/student.S-T
;;
*)
PARENT=/usr/users/student.U-Z
;;
esac
echo $PARENT
echo '\nAdding new user ' $NAME '\n'
if test \! -d /usr/spool/mail
then
mkdir -p /usr/spool/mail
chmod 1777 /usr/spool/mail
fi
/usr/sbin/useradd -m -u "${UID}" -g "${LOGGROUP}" -c "${NAME}" \
-d "${PARENT}/${USERNAME}" \
-s "${LSHELL}" -k "${SKEL}" ${USERNAME}
if [ "${?}" -ne 0 ]
then
echo 'Error, exiting useradd %1$s. Check /etc/passwd.\n' "${0}"
exit 1
fi
cd "${PARENT}"
if [ "${?}" -ne 0 ]
then
echo 'Unable to cd to parent directory %1$s\n' "${PARENT}"
exit 1
fi
if [ -d "${USERNAME}" ]
then
chmod 751 ${USERNAME}
fi
# Script to set up staff and faculty accounts for CAP,
#
echo $USERNAME >> /usr/local/majordomo/lists/students
echo $FNAME'.'$LNAME':'$USERNAME >> /usr/adm/sendmail/aliases
/usr/sbin/newaliases
/usr/local/bin/scmkpw $USERNAME
#add new line to the userlist
echo $USERNAME', '$FNAME'.'$LNAME'@simpson.edu' >> /usr/local/etc/userlist
#Set up student accounts for CAP and create file mbox
#and perform some small admin tasks
#
#
cd $PARENT
cd $USERNAME
rmdir bin
rm -rf system*
touch mbox
chown $USERNAME mbox
edquota -p standard -u $USERNAME
trap - 1 2
done
|