Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am new to unix but have a basic understanding.
I have a script that reads in a username then uses useradd to create a logon, but if you type a user name then delete it the system tries to change the logged on as.
How can I set up a script that stops either a null value or the current user from going any further?I currently have the following written in SCO
echo Enter user login name
read loginID
echo " "
echo Enter Full user name
read username
echo " "
gid_def=group share_root=/u/shared
# Checks to see if the user exists in the passwd file
cut -d":" -f1 /etc/passwd | egrep '^'$loginID'$' > /$data/nuidlog
# Looks to see if the nuidlog contains data
datasize=`ls -l /$data|tail -1|cut -c 40-41`
if [ $datasize = 0 ]
then
#Create user script
clear
echo "Adding user... $loginID"
/etc/useradd -G$gid_def -c"$username" -d /usr/$loginID $loginID
echo "Making user directory..."
mkdir /usr/$loginID
cp $data/.profiletemp /usr/$loginID/.profile
echo "Creating user profile..."
echo " "
passwd $loginID
echo $helpdeskuser "created " $loginID " at " $datenow >> $log/log
else
echo $loginID ' already in system '
sleep 5
fi

#!/bin/sh
echo Enter user login name
read loginID# if you're using ksh you can
# do this
#if [[ -z $loginID ]]
#then
# echo "null login"
# exit
#fi# if using sh, try this:
# X will equal X if null
if [ X$loginID = "X" ]
then
echo "null login"
exit
fi# Try this for checking if user exists
while read line
do
if [ `echo $line|cut -d ":" -f1 ` = $loginID ]
then
echo "$loginID already exists"
exit
fi
done < /etc/passwd

Thanks very much nails I was using sh, I never knew you could run the if like that, I am sure I will use that for many more scripts to come.
I did have trouble with the checking if the user exist though, it was a bad statment probably me putting it in the wrong place ;) Can you explain how that script works as I have never seen a done with a read in before.
Newbie in Trouble.

Hi:
Are you asking to explain this:
if [ X$loginID = "X" ]
then
echo "null login"
exit
fiFirst, the spaces around the braces is very important to the shell. This will fail:
if [X$loginID = "X"]
then
.
.When appending a variable to another string you create a new string. In the case above, it the contents of loginID is empty/null, then appending the variable didn't change "X". You then know loginID is null and can take appropriate action.
The more modern shells have null checks like -z. There also -n which returns true if the length of the variable is zero.
Regards,
Nails

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

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