LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2008, 05:05 AM   #1
Kai'
LQ Newbie
 
Registered: Apr 2008
Distribution: Redhat; Fedora
Posts: 7

Rep: Reputation: 0
Exclamation Arrays in useradd command


ok, so I tried using arrays to create X amount of users. I made an arrayname called 'users' using declare -a. I set the variable user to:

(smith ocelot derek)

and then I use the useradd command but I wanted to use a value that represented all the values rather than listing arrays like this:

$ useradd ${name[0]}
$ useradd ${name[1]}
$ useradd ${name[2]}

or, I also tried using a file called 'user' using the same structure
as the usenames above but is this form:

(smith
ocelot
derek)

and using the cat command...

$ cat user|useradd

but that didn't work, so what would be the best way to add all the users at
one command (help on either method would be appreciated)
 
Old 04-29-2008, 05:45 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You cannot create more than one user at a time with useradd command, so you have to cycle over the content of the array and execute the useradd command on each element. You can use a for loop:
Code:
for i in $(seq 0 10)
do
  echo useradd ${users[$i]}
done
If you have a list of user names stored in a file, you can read the file one line at a time using a for loop like this
Code:
for name in $(cat testfile)
do
  echo useradd $name
done
 
Old 04-29-2008, 06:22 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
If you have an array named "usr":

Code:
for i in `seq 0 $((${#usr[*]}-1))`; do useradd {usr[$i]};done
${#usr[*]} gives you the size of the array, as does ${#usr[@]}

I could have added 50 users by hand in the time it took to figure this out......
<<edit>> or colucix could have posted the same basic answer but much faster......!!

Last edited by pixellany; 04-29-2008 at 06:24 AM.
 
  


Reply



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
useradd command sophisticate Linux - Newbie 2 01-06-2008 11:49 AM
useradd command Linuxien Linux - General 2 11-14-2006 07:32 AM
useradd command Wolvastur Linux - Newbie 9 01-25-2005 10:01 AM
useradd command johnpayback Linux - General 4 08-13-2002 05:24 PM
useradd command allancondino Linux - General 5 04-27-2002 12:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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