LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-30-2014, 05:39 AM   #1
NerdGZ
LQ Newbie
 
Registered: Feb 2014
Posts: 12

Rep: Reputation: Disabled
Add multiple users on linux via file.


Hello Everyone,

i created a script to add user automatically, and i wanted to difine the option separate with : or ;

As a default on my script i have it with space.

see the script:

#! /bin/bash

NEW_USERS="/home/nerd/userlist.txt"
HOME_BASE="/home/"

cat ${NEW_USERS} | \
while read USER GROUP COMMENT PASSWORD ;

do
useradd -c ${COMMENT} -G ${GROUP} -m -d ${HOME_BASE}${USER} ${USER}
echo $USER
echo $PASSWORD | passwd $USER --stdin
echo; echo "User $USER password change!"
chage -d 0 $USER
done

THIS is the userlist.txt

es666d group1 Nerd-GZ init1234


i would like to user the option Nerd-GZ without -
On the script is not possible because the option are only allow to be separate with space.

i think is something with the option on the script:

while read USER GROUP COMMENT PASSWORD

Could you please help me with that?

Thank you.
Nerd
 
Old 07-30-2014, 06:12 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Sure

Code:
#! /bin/bash

NEW_USERS="/home/nerd/userlist.txt"
HOME_BASE="/home/"

while IFS="," read USER GROUP COMMENT PASSWORD;do
    useradd -c "${COMMENT}" -G ${GROUP} -m -d ${HOME_BASE}${USER} ${USER}
    echo $USER
    echo $PASSWORD | passwd $USER --stdin
    echo; echo "User $USER password change!"
    chage -d 0 $USER
done < "$NEW_USERS"
in your input file use "," instead of " " to separate the fields
the IFS="," is making the while read use , as the Input Field Separator
To protect from wordsplitting, wrap the field which contains a space with ""

Your input file would look like this

Code:
es666d,group1,Nerd GZ,init1234
 
Old 07-30-2014, 06:35 AM   #3
NerdGZ
LQ Newbie
 
Registered: Feb 2014
Posts: 12

Original Poster
Rep: Reputation: Disabled
Hi Firerat,

Thank you very much. It is working!!!
 
Old 07-30-2014, 06:49 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Glad to read it

The important thing is to understand *why* it works


I should also comment on a couple of other things with your script.

Currently you are using CAPITALS for the variables, not a real problem but should be avoided
The reason: "System wide" varailbles are in CAPS, and you want to avoid mixing them up
For example,
Code:
echo $USER
Notice that I also used "heredocs" to get the input file instead of using cat.


Some links to help you out

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashGuide
http://www.gnu.org/software/bash/manual/bashref.html

The tldp stuff is great, however there are some bad habits in it,
The mywiki.wooledge does a very good job of 'fixing' those habits
 
  


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
[SOLVED] Add multiple users to a secondary group smilemukul Linux - Newbie 1 09-21-2013 05:40 PM
add multiple users(Script) amer_58 Programming 22 02-22-2013 09:09 AM
Add users to Multiple Computers at Once? DiskChris Linux - General 3 12-16-2012 09:39 AM
How to setup file permissions for multiple groups/users that use Windows AND Linux unassassinable Linux - Security 4 11-02-2010 08:00 PM
Can't add new users from an input file hindll01 Programming 4 09-07-2004 07:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:23 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
Open Source Consulting | Domain Registration