- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: script to modify a list of users
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 08:23 AM
тАО06-05-2008 08:23 AM
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
Solved! Go to Solution.
- Tags:
- usermod
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 08:46 AM
тАО06-05-2008 08:46 AM
SolutionYou 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.
- Tags:
- cut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 08:59 AM
тАО06-05-2008 08:59 AM
Re: script to modify a list of users
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 09:03 AM
тАО06-05-2008 09:03 AM
Re: script to modify a list of users
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2008 09:27 AM
тАО06-05-2008 09:27 AM