Operating System - Linux
1752733 Members
5494 Online
108789 Solutions
New Discussion юеВ

Re: script to modify a list of users

 
SOLVED
Go to solution
JLee_1
Advisor

script to modify a list of users

Hello, All -

I need to modify a bunch of user accounts to add full names to their profiles. I'm tyring to put something together that will let me read the username and full name in from a comma delimited file:

uid1, User Number 1
uid2, User Number 2
uid3, User Number 3

etc...

The loop I've tried to create just hangs on me, and I know I'm missing something...probably a couple things. I'm hoping someone can take a look at this and give me some pointers. I'm really green with scripting...obviously.

while read INPUTFILE
username=`echo $INPUTFILE | cut -d , -f 1`
name=`echo $INPUTFILE | cut -d , -f 2`
do
usermod -c $username $uid
done

"INPUTFILE" being the file with the usernames and full names.

Thanks in advance
4 REPLIES 4
Hein van den Heuvel
Honored Contributor
Solution

Re: script to modify a list of users

Your biggest problem is probably a space between the -d and the comma in the cut command.
You also set up 'name' but use 'uid'

Might I recommend testing scripts with 'echo' or 'print' in front of the command itself? That way you can try a few times until you get it right without creating damage.

Personally I actually tend to prefer a (awk, perl, sh, dcl) script to generate the list of commands in a file, then review that, chmod +x and execute.

One of many possible solutions based on your start:

$ cat x
uid1, User Number 1
uid2, User Number 2
uid3, User Number 3

$ cat x.sh
while read INPUTLINE
do
username=$(echo $INPUTLINE | cut -d, -f 1)
uid=$(echo $INPUTLINE | cut -d, -f 2)
echo usermod -c $username $uid
done

$ ./x.sh < x
usermod -c uid1 User Number 1
usermod -c uid2 User Number 2
usermod -c uid3 User Number 3

And a solution along the lines of the other method I suggest, using awk:

$ awk -F, '{print "usermod -c ", $1, $2}' x > todo
$ cat todo
usermod -c uid1 User Number 1
usermod -c uid2 User Number 2
usermod -c uid3 User Number 3

hth,
Hein.


Patrick Wallek
Honored Contributor

Re: script to modify a list of users

Why make it more complicated than it needs to be?

Step 1 -- Omit the commas separating the data.

Step 2 --

while read UID USERNAME
do
echo ${UID}
echo ${USERNAME}
## usermod -c ${USERNAME} ${UID}
done < INPUTFILE

That script will take the first field and assign it to UID and then take the rest of the line and assign it to USERNAME. This depends on the file having everything separated by SPACES only though, hence my step 1 to remove the commas from the file.

Once you are satisfied with the output from the echo statements, you can uncomment the usermod so it actually makes your changes.

Much much easier than try to awk, cut, etc.
Matti_Kurkela
Honored Contributor

Re: script to modify a list of users

Your "while read..." loop is trying to read from the standard input stream.

In the "read INPUTFILE" command, INPUTFILE is the name of the variable where the result of the read operation is stored, not the name of the file the command is trying to read.

The "do" line should come immediately after the "while" line. The "username=" and "name=" variable assignments should be in between the "do...done" pair.

Your usermod command is a bit confused: it would try to store the value of the "username" variable as the real name of the user specified in the "uid" variable. But "uid" has not been defined yet, so the shell will replace it with an empty string, "".

Your script with minimum necessary modifications:

while read LINE
do
USERNAME=`echo "$LINE" | cut -d , -f 1`
FULLNAME=`echo "$LINE" | cut -d , -f 2`
usermod -c "$FULLNAME" "$USERNAME"
done < INPUTFILE

MK
MK
JLee_1
Advisor

Re: script to modify a list of users

Thanks a lot, guys - I appreciate the quick responses. I ran through a couple of the suggested ways of writing it out and it's working well. Much appreciated!