Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-09-2014, 11:24 PM   #1
LQ Newbie
Registered: Jun 2014
Posts: 3

Rep: Reputation: Disabled
Question adding users and passwords from a .txt file help

running linux mint 16
I have the following shell script i was using in Vi to add users and passwords from a text file:

# run script as ./ /home/ehewitt1/addingusers.txt
while read str
read -a arr <<< $str
echo useradd ${arr[0]}
useradd ${arr[0]}
echo ${arr[0]}:${arr[1]}
echo ${arr[0]}:${arr[1]} | chpasswd
done <$1

getting the following error:
./useraddition: line 10: $1: ambiguous redirect
any thoughts?? thanks
Old 07-10-2014, 08:49 AM   #2
Registered: Mar 2013
Posts: 622

Rep: Reputation: 136Reputation: 136
Suppose you have file with usernames and passwords like
user1 pass1
user2 pass2
user3 pass3
user4 pass4
user5 pass5
user6 pass6
user7 pass7
user8 pass8
user9 pass9
user10 pass10
username should be written at first column and after space, password of the user should be written.
Now if you run following script,
lst1=$(cat /path/of/file/a.txt)
for i in $lst1
a1=$(echo $i | awk '{print $1}')
useradd $a1
a2=$(echo $i | awk '{print $2}')
echo -e "$a2\n$a2" | passwd $a1
It creates 10 users and sets their requested passwords.
All usernames should be new and unique. No user should be existed before in the system as well as no group should be created before with same username.
Because when you create user, it creates group with same name.

Last edited by eklavya; 07-10-2014 at 08:52 AM.
Old 07-10-2014, 11:36 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Actually the error $1: ambiguous redirect means you didn't pass any argument to the script, otherwise the shell substitution should do its work and feed the loop with the file specified in the command line.

What puzzles me is the redundancy of the read command:
while read str
  read -a arr <<< $str
Shouldn't it be simpler something like this?
while read user passwd
  echo $user $passwd
done < $1
provided the file contains user name and password in each line, as in the example provided by eklavya above.
Old 07-10-2014, 09:04 PM   #4
LQ Newbie
Registered: Jun 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
yes that txt file is pretty much what it is using tab to space password from user. redundant yes. I'm taking my first linux class and have been having trouble with this one. and when I got it to work I still couldn't log in as user and password I echo'd to the screen. kept getting authentication failure


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
adding users and passwords with adduser sean04 Programming 3 03-12-2012 04:11 AM
[SOLVED] Adding file extensions to listings in a txt file mrjawz Linux - Newbie 5 02-15-2012 09:53 AM
Automate adding users and passwords linuxy2 Linux - Security 1 03-03-2007 02:32 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
adding users and passwords to mysql caged Programming 1 10-27-2004 06:22 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration