LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 06: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, 07:12 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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, 07: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, 07:49 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

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


All times are GMT -5. The time now is 07:59 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