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

bart simpson , andHisPassword
maggy simpson , andHisPassword
#!/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

What are the usernames suppose to look like?

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.

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

You have to read file line by line and then create user

root@nio:/tmp# cat file
bart_simpson,andHisPassword
maggy_simpson,andHisPassword
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

root@nio:/tmp# sh adduser.sh file
User 'bart_simpson' has been added to system!
User 'maggy_simpson' has been added to system!
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

akshay@nio:/tmp$ su bart_simpson
Password: andHisPassword
$ whoami
bart_simpson
1 Like

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

while IFS=, read username password; do
..
..
done <"$@"
1 Like

Thanks, I'll test this code

Hello, I am quite new to linux and was wondering what this line of code did?

Thank you very much!

password= Assignment to a shell variable
$() call to a subprocess
perl -e call the perl binary to execute code from command line, instead of file source code
'print crypt($ARGV[0], "password")' display the result of calling the perl function crypt
crypt($ARGV[0], "password") crypt will return a digest of whatever was given as argument. $ARGV[0] is the first argument from the command line, in this case the last $password term. "password" is what's called SALT string. It can be anything. Not to confuse with $password
$password Argument given at the command line, in the form of perl variable, to crypt (presumably a string that represents a clear text password)

Best way of learn is to try:
Change password and random_text to anything you want

perl -e 'print crypt("password", "random_text")'
2 Likes

Another solution is to use openssl to generate encrypted passwords (it automatically chooses salt values for you, but you could duplicate what the perl code was doing by usint the -salt pa option

password=$(openssl passwd "$password")
password=$(openssl passwd -salt pa "$password")

Notice how the $password variable is quoted above, this is to protect against white space in the password.