ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need a script to add all users from a exported excel dokument to text file with tabbed or comma seperated format.
I've got the following fields
Username Groupname firstname aftername dateofbirth city
Now I need to make the script
add a user with username and set the password as the same as the username
then add it to the groupname and add all that addinitnal info.
I'm not used with scripting and I dont know what language.(Bash will do?)
Could anyone help me out here?
I known useradd and the different paramaters if I was running them as a command but I'm clueless how to make it a file that I can simply run that will add all these users (about 40)
A bash script should do the job, with a little help from awk, of course. It is way too many beers into the evening for me to give much help, but just look up some stuff on awk--it isn't too hard to pull info out of a cvs file and redirect it to a regular terminal command like adduser (not exactly sure on how to solve the whole new-password problem though). I'll look into more details for you tomorrow when the screen stops going all blury.
#!/bin/sh
# WARNING: USE AT YOUR OWN RISK!!!
FILE=test.txt # Name of the file to parse
SHELL=/bin/bash # Default shell
USERADD=/usr/sbin/useradd # Location of useradd
if [ -f $FILE ]; then # Make sure the file exists
# White space and/or tab separated... Best not to use this because
# if you have a city name with a space (ie Las Vegas), then it will
# not be correct
#while read user group first after dob city
while IFS=, read user agroup first after dob city # Comma separated
do
if [ "$user" != "" ]; then # Make sure we have a username (may want to enhance this test)
echo "$user $agroup $first $after $dob $city"
#echo
#$USERADD -s $SHELL -m -G $agroup -c "$first,$after,$dob,$city" -p $user $user
if [ $? -gt 0 ]; then
echo "Error adding user: $user"
else
echo "Added user: $user"
fi
fi
done <$FILE
else
echo "No such file: $FILE"
fi
It's just meant to show you one way of doing it, it doesn't do much in the way of sanity checks. But if you are absolutely certain of the integrity of the file, and it's format, that you are parsing, then it will work. The file format is "assumed" to be exactly as you said it would be, ie comma separated with every line in it being valid input. It does check for blank lines, but nothing beyond that. And as noted in the script, you shouldn't use tab delimited lines since it's possible for city names to have spaces and to the script a space is the same as a tab. It also assumes the additional group you are adding them to already exists. If it doesn't then see "man groupadd" and add code for that. Anyway, you may want to take a look at a shell scripting guide and add some more sanity checks and etc. Here's a pretty good one: http://www.tldp.org/LDP/abs/html/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.