Adding users from a txt fille


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding users from a txt fille
# 1  
Old 10-14-2014
Adding users from a txt fille

hello i'm making a bash script for adding users from a txt fille

i have a basic script that adds users and their password . when you type the users by hand , now i want to upgrade my script with a txt file of users and their password , but i don't know how to start .

my txt file looks like this

Code:
bart simpson , andHisPassword
maggy simpson , andHisPassword

Code:
#!/bin/bash # Script to add a user to Linux system 
if [ $(id -u) -eq 0 ]; then 	
read -p "Enter username : " username 	
read -s -p "Enter password : " password 	
egrep "^$username" /etc/passwd >/dev/null 	
if [ $? -eq 0 ]; then 		
   echo "$username exists!" 		
   exit 1 	
else 		
  pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) 		
  useradd -m -p $pass $username 		
  if  [ $? -eq 0 ]; then  
     echo "User has been added to system!" 
  else
     echo "Failed to add a user!" 	
  fi 
  else 	
      echo "Only root may add a user to the system" 	
      exit 2 
fi

Moderator's Comments:
Mod Comment in order to make code readable us other humans please don't use odd formatting

Last edited by rbatte1; 10-14-2014 at 01:51 PM.. Reason: Added CODE tags for input file
# 2  
Old 10-14-2014
What are the usernames suppose to look like?
# 3  
Old 10-14-2014
You had a syntax error, possibly because it is hard see them in run-on lines of code.
suggestions:

check for root user first - use the id command
Why are you encoding the password? Use chpasswd.
# 4  
Old 10-14-2014
I want a script that adds users from a txt file. From this file I want to take first name , last name and passwords, I have this basic schript I want to expand, but I'm not sure how to begin , So where I now manually enter the data, I would now like to get this from the text file


@jim mcnamara thanks for the tip
# 5  
Old 10-14-2014
You have to read file line by line and then create user

Code:
root@nio:/tmp# cat file
bart_simpson,andHisPassword
maggy_simpson,andHisPassword

Code:
root@nio:/tmp# cat adduser.sh
#!/bin/bash

if [ $# -lt 1 ]
  then
    echo "Usage : $0 userfile"
    exit
fi

if [ $(id -u) -eq 0 ]; then 	
	

while IFS=, read username password; do

	if egrep "^$username" /etc/passwd >/dev/null;  then 		
   		echo "$username exists!" 			
	else 		
  		password=$(perl -e 'print crypt($ARGV[0], "password")' $password) 		
  		
		if  useradd -m -p "$password" "$username" >/dev/null; then  
     			echo "User '$username' has been added to system!" 
  		else
     			echo "Failed to add  user '$username'!" 	
  		fi 
	fi
done <"$@"

else 	
      echo "Only root may add a user to the system" 	
      exit 
fi

Usage
Code:
root@nio:/tmp# sh adduser.sh file
User 'bart_simpson' has been added to system!
User 'maggy_simpson' has been added to system!


Code:
root@nio:/tmp# tail -2 /etc/passwd
bart_simpson:x:1001:1002::/home/bart_simpson:/bin/sh
maggy_simpson:x:1002:1003::/home/maggy_simpson:/bin/sh

---------- Post updated at 02:11 AM ---------- Previous update was at 02:02 AM ----------

Test
Code:
akshay@nio:/tmp$ su bart_simpson
Password: andHisPassword
$ whoami
bart_simpson

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 10-14-2014
Thanks for the quick help, I just do not understand where the code is
where the script searches in the text file after the names
, i see you named your txt file , file
# 7  
Old 10-14-2014
Quote:
Originally Posted by Roggy
Thanks for the quick help, I just do not understand where the code is
where the script searches in the text file after the names
, i see you named your txt file , file
See here we read input file

Code:
while IFS=, read username password; do
..
..
done <"$@"

This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Extra Column in txt file base on Condition

HI Guys, I have below input. Output Base on Below Condition. 1> if forth column is empty and next coming line have same name with \es then add that column name on all rows 2>rest of all are es:vsDataEUtranCellFDD Input:- CCL01736 CCL01736_7A_1 es:vsDataEUtranCellFDD ... (3 Replies)
Discussion started by: pareshkp
3 Replies

2. Shell Programming and Scripting

Simple script for adding users

Hi guys, I've a simple linux script (made by my friend), which adds users to the system from userlist file. it also creates user home dir and copies certain files to the directory. To be honest, am a newbie in scripting so am unable to fully understand how the script is working. unfortunately,... (30 Replies)
Discussion started by: vish6251
30 Replies

3. UNIX for Dummies Questions & Answers

Adding new columns to txt files

Dear all, I have a question. I have a txt.file as below. i want to add 3 more columns: column3=conlum 2*column2; column4=(1-column2)*(1-column2); column5=1-column3-column4. Do you know how to do it? Thanks a lot! file: column1 column2 a 1 b 20 c 30 d 3 ... (2 Replies)
Discussion started by: forevertl
2 Replies

4. AIX

adding users via smit

I apologize if this is a simple/stupid question. When I add users in smit as root, many(most) of the fields are automatically popluated with some basic default values. Some other admins here have access to create users via sudo, however when they create users (sudo smit users), the user gets... (3 Replies)
Discussion started by: mshilling
3 Replies

5. AIX

Adding column in a .txt file

Helle, I want to create a .ksh script in order to realize the following : I have a .txt file organized in a bloc of information, each bloc start with 000 as following: 000... 001... 003... 004... 000... 001... 003... 004... . . My aim is to add a new... (6 Replies)
Discussion started by: zainab2006
6 Replies

6. UNIX for Dummies Questions & Answers

Adding users question

Hello there, I want to add new users to my system, so, being logged in as root I do useradd -m user_name, and the new user is added to the system. The problem is that it has more privileges than I expected. If I do su user_name then I am allowed to do cat /etc/passwd , so it is... (4 Replies)
Discussion started by: help.goes.here
4 Replies

7. UNIX for Dummies Questions & Answers

Adding EMPTY columns to Tab-delimited txt file

Hi I have a txt file with 4 columns where I need to add 4 empty columns in the middle meaning that I need what is currently column 4 to be column 8 in a new file. The idea is that I have to use the file as input in a program that reads the data in column 1 and 8, so the content of the other... (8 Replies)
Discussion started by: Banni
8 Replies

8. Shell Programming and Scripting

Adding delimiter to logged in users

Hi guys! Just was wanting to run a command that would allow me to seperate the currently logged in users. Basically from this format: user1 user2 user3 To: user1|user2|user3 (Note the lack of a pipe at the end, not sure if thats possible) Basically it needs to be in this... (11 Replies)
Discussion started by: crawf
11 Replies

9. UNIX for Dummies Questions & Answers

Adding users to /etc/group

I'm using SAM to add users on an HP and they're adding fine. But in /etc/group it only lists the group names. It's not adding the users in there. Is there a way to have them put in there without going into SAM and modifying the group and adding them? I guess what I want to happen is when I add... (1 Reply)
Discussion started by: golfhakker
1 Replies

10. Shell Programming and Scripting

Adding users

Anyone have a simple shell script that will prompt and accept screen input for each field that is required in the /etc/passwd file? (3 Replies)
Discussion started by: Relykk
3 Replies
Login or Register to Ask a Question