Computing.Net > Forums > Unix > New user script problem

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.

New user script problem

Reply to Message Icon

Name: pobster
Date: December 9, 2004 at 04:51:36 Pacific
OS: XP SP2
CPU/Ram: AMD Athlon 2000+
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: December 9, 2004 at 09:31:39 Pacific
Reply:

#!/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


0

Response Number 2
Name: pobster
Date: December 9, 2004 at 14:47:48 Pacific
Reply:

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.


0

Response Number 3
Name: nails
Date: December 10, 2004 at 11:19:05 Pacific
Reply:

Hi:

Are you asking to explain this:

if [ X$loginID = "X" ]
then
echo "null login"
exit
fi

First, 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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: New user script problem

scripting problem (HELP!!!) www.computing.net/answers/unix/scripting-problem-help-/5360.html

Shell script problem www.computing.net/answers/unix/shell-script-problem/4434.html

new user www.computing.net/answers/unix/new-user/8245.html