1

I have 2 files. vi usernames

user2
user3
user4

vi passwords

pw2
pw3
pw4

I make script that add users and set passwords form files.

#!/bin/bash

for i in `cat /root/o/usernames`;
do
       if [ -z `getent passwd $i` ]
       then
               useradd $i
               echo user $i added
               cat /root/o/passwords | passwd $i --stdin
       else
               echo user $i is exist
       fi
#done

when this script is run add users and set password pw1 all users. But i want to set multiple password each user. How can i make?

How can i make set multiple password each users from file?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Please take a look at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting). – Cyrus Aug 13 '23 at 15:12
  • Do you want multiple passwords for each user, or do you want the n'th password set for the n'th user? – Jeppe Aug 13 '23 at 15:15
  • [Shellcheck](https://www.shellcheck.net/) identifies several problems with the code. The report includes links to more information about the problems and how to fix them. It's a good idea to run [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code. – pjh Aug 13 '23 at 21:01
  • You always pipe the whole o/passwords file into `passwd`, which then takes of course the first line. You basically have to loop over the usernames and passwords file in parallel. There are several ways to do this. For instance, you could store the usernames and passwords into an array and loop over the arrays. Don't forget to add an error handling for the case that the number of users is different to the number of passwords. – user1934428 Aug 14 '23 at 08:59
  • I want n'th password set for the n'th user. – Nicat Rasulov Aug 15 '23 at 07:22

2 Answers2

2

in my scenario I'll reads two files, usernames and passwords, which contain the usernames and passwords respectively and it uses a while loop to iterate through each line of the files and for each line, the script checks if the user already exists using getent passwd "$username",if the user exists,it displays a message saying that the user already exists and if the user does not exist,my script creates the user using useradd "$username",so it then sets the password for the user using echo "$username:$password" | chpasswd and at the end the my code displays a message indicating that the user was added with the corresponding password!

check this out :

#!/bin/bash

while IFS= read -r username && IFS= read -r password <&3; do
    if getent passwd "$username" >/dev/null; then
        echo "User $username already exists"
    else
        useradd "$username"
        echo "$username:$password" | chpasswd
        echo "User $username added with password $password"
    fi
done < /root/o/usernames 3< /root/o/passwords
Freeman
  • 9,464
  • 7
  • 35
  • 58
1

To create the users, you could use xargs to execute useradd (here with options -m and -G wheel as demo) once per input line (as per -L1).

xargs -L1 useradd -mG wheel < usernames.txt

To change their passwords, you could use paste to linewise concatenate both files, using -d: to set a colon in between. Then, read this output with chpasswd, which "reads a list of user name and password pairs from standard input and uses this information to update a group of existing users".

paste -d: usernames.txt passwords.txt | chpasswd
pmf
  • 24,478
  • 2
  • 22
  • 31
  • Sorry, İ cant understand what is L1? – Nicat Rasulov Aug 15 '23 at 08:57
  • @NicatRasulov `-L` specifies how many lines are taken at once per executed command. As you want to execute `useradd` for each username individually, its value is `1`. – pmf Aug 15 '23 at 09:02