LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2004, 12:34 AM   #1
pmrent
LQ Newbie
 
Registered: May 2004
Location: Green Bay, WI
Distribution: RH 9 RHE 2.1 3.0
Posts: 27

Rep: Reputation: 15
Question for i in `cat 1000_user_file`


I'm trying to add 1000 users to my system. I have the Users in a text doc. There is one name (first and last) per line. So I figured I would write a little for loop.

for i in `cat 1000_user_list`
do
useradd $i

blah blah blah

However, the loop is taking the first name and then the last name. So for user Joe Smoe I get a user joe and a user smoe.

How do I tell the shell script to take the whole line at a time?

Please advise.
Thank You

Pete
 
Old 09-06-2004, 01:15 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
First, you might want to ask yourself if Joe Smoe is a valid user name. Normally, you could read more than one section of the file by using quotes ( "Joe Smoe" )
maybe something like this...

NEW_USERS="/home/names.txt"
cat ${NEW_USERS} | \
while read "USER"
do

but I think you would have better luck with joe_smoe
 
Old 09-06-2004, 06:58 AM   #3
pmrent
LQ Newbie
 
Registered: May 2004
Location: Green Bay, WI
Distribution: RH 9 RHE 2.1 3.0
Posts: 27

Original Poster
Rep: Reputation: 15
sorry. I should have put more of the code in my original posting. You are absolutely correct about the user name. I have to make the user ID 8 letters of the last name and the first letter of the first name. So Joe Smoe will be smoej.

But in order to get my cut cmd to work I still need to get the whole name out of the txt doc so I can use it.

so the script will look something like:

i="Smoe Joe"
LASTNAME=`echo $i |cut -d" " -f1`
USERIDLAST=`echo $LASTNAME |cut -c1-8 |tr [:upper:] [:lower:]`
FIRSTNAME=`echo $i |cut -d" " -f2`
USERIDFIRST=`echo $FIRSTNAME | cut -c1 |tr [:upper:] [:lower:]`
FULLNAME="$LASTNAME, $FIRSTNAME"
USERID=$USERIDLAST$USERIDFIRST
SECGROUP=users
PRIMGROUP=hr
PASSWD=123456

echo $i
echo $LASTNAME
echo $USERIDLAST
echo $FIRSTNAME
echo $USERIDFIRST
echo $SECGROUP
echo $PRIMGROUP

echo useradd -c "$FULLNAME" -g "$PRIMGROUP" -G "$SECGROUP" $USERID

#Setting the users password
echo "echo $PASSWD | passwd --stdin $USERID > /dev/null"

ignore the echos of the variable's. I'll remove them when I get this done. Just testing right now.

So I have everything pretty much set except how to get the list of users in this.

Not the prettiest script in the world but not bad for a newbie?

Pete

Last edited by pmrent; 09-06-2004 at 08:02 AM.
 
Old 09-06-2004, 08:28 AM   #4
NetSnake
LQ Newbie
 
Registered: Sep 2004
Location: China
Distribution: Debian 3.0r2 unstable
Posts: 28

Rep: Reputation: 15
I suggest that you can use the "Expect" language to complete your work :-)
 
Old 09-06-2004, 08:41 AM   #5
pmrent
LQ Newbie
 
Registered: May 2004
Location: Green Bay, WI
Distribution: RH 9 RHE 2.1 3.0
Posts: 27

Original Poster
Rep: Reputation: 15
I have never heard of Expect.

What is that?
 
Old 09-06-2004, 09:18 AM   #6
slackist
Member
 
Registered: Feb 2004
Location: Phuket
Distribution: Slackware 14.1 and Slackware Arm
Posts: 466

Rep: Reputation: 40
Quote:
Originally posted by pmrent
I have never heard of Expect.

What is that?
I thought NetSnake was trolling, but a brief google search

http://www.google.com/search?q=expec...utf-8&oe=utf-8

showed up some results, including books
mark

/apologies to NetSnake
 
Old 09-06-2004, 09:48 AM   #7
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
I wonder if you would have better resutts to edit the names file to use the smoej naming scheme. Then it would work with the script I have already made.

The file /home/names.txt has the user names (lower case) , groups and the Linux/Samba password.
The Linux and Samba passwords are the same and the user should change them soon after login.
The groups can already exist.

For example.....

fred service 12qwaszx
sam hr 34erdfcv
joe maint 56tyghbn
sally sales 78uijkm

Code:
#!/bin/bash
#
# Ensure that root is running the script.
##
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
echo "You must be root to add news users!"
exit 1
fi
#
clear
NEW_USERS="/home/names.txt"

cat ${NEW_USERS} | \
while read USER GROUP SMBPASS ; do
   
   groupadd ${GROUP} 2> /dev/null
   adduser ${USER} -g ${GROUP}

   (echo $SMBPASS; echo $SMBPASS) | passwd --stdin ${USER} > /dev/null
   echo Added user ${USER}

   smbpasswd -e ${USER} -w ${SMBPASS} > /dev/null
   (echo $SMBPASS; echo $SMBPASS) | smbpasswd -as ${USER}
   echo -e "${USER} = ${USER}" >> /etc/samba/smbusers
done

Last edited by homey; 04-05-2005 at 03:31 PM.
 
Old 09-06-2004, 02:04 PM   #8
pmrent
LQ Newbie
 
Registered: May 2004
Location: Green Bay, WI
Distribution: RH 9 RHE 2.1 3.0
Posts: 27

Original Poster
Rep: Reputation: 15
We have lost track of my original question.

How do you get the a for loop to read the whole line, not word for word. I know it can be done. I've done it before.
I just had to use some brackets and some quotes or something like that.

Any idea's?
 
Old 09-06-2004, 02:18 PM   #9
nitin_batta
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat Enterprise Server 2.1
Posts: 96

Rep: Reputation: 15
Hey Dude,

this is could help you ... assuming line is my variable.

Code:
while read line
do
echo $line > /home/nitin/temp
fname=`awk '{print $1}' temp`
lname=`awk '{print $2}' temp`
<You can use your cut script here since now you have the first name and last name >
done < /home/nitin/user_list
I am assuming that /home/nitin/user_list is your file name and it has entries as

Quote:
joey smith
fname lname
 
Old 09-06-2004, 02:20 PM   #10
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
The default field seperator includes a space. Just set it to a newline before your loop with these 2 lines:
IFS="
"
 
Old 09-06-2004, 10:23 PM   #11
pmrent
LQ Newbie
 
Registered: May 2004
Location: Green Bay, WI
Distribution: RH 9 RHE 2.1 3.0
Posts: 27

Original Poster
Rep: Reputation: 15
Finally the answer I was looking for.

Thank You David

Your the Bomb!!!!!!!!!!!!!
 
Old 09-06-2004, 10:24 PM   #12
karunesh
Member
 
Registered: Jan 2002
Location: India
Distribution: CentOS/Mandriva
Posts: 126

Rep: Reputation: 15
pmrent..

I remember I done this thing long back..but with a trick...

1. First open your userlist.txt file and replace all SINGLE SPACE with something like +, you can do this in a single go by using vi editor %s command.

2. Now you userlist.txt file looks like this.

Bill+Joe
Nike+Johns
.
.
.

3. In your shell script use the cut command with proper filter to cut the number of columns in what ever way you like.

4. Here you can use sed to replace all + with SINGLE BLANK SPACE.For example...
Quote:
for lnam in `cat userlist | cut -c 7-`
do
lnam=`echo "$lnam" | sed 'y/+/ /'`
fname["$i"]="$lnam"
let "i += 1"
done
5. Now you can use variable fname what ever way you like.

This is a thought, might be helpful...
 
Old 09-06-2004, 10:33 PM   #13
pmrent
LQ Newbie
 
Registered: May 2004
Location: Green Bay, WI
Distribution: RH 9 RHE 2.1 3.0
Posts: 27

Original Poster
Rep: Reputation: 15
That idea makes sense to me too Karunesh.

Thanks for your help.

Pete
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My Cat unimaginative General 71 02-18-2004 02:08 PM
Cat ... Beuzekom Linux - Newbie 4 01-19-2004 06:32 PM
Any cat gurus in here? deepsix Linux - General 14 08-28-2003 10:41 AM
cat notsoevil Linux From Scratch 3 02-14-2002 11:16 AM
regarding cat gui10 Linux - Security 6 12-07-2001 09:09 PM


All times are GMT -5. The time now is 08:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration