Home > Enterprise >  Add multiple new users - Bash Shell Scripting
Add multiple new users - Bash Shell Scripting

Time:09-22

I am looking to add multiple users to a UNIX Red Hat system. I am using two text files as sources containing 9 usernames and 9 full names separated individually on 9 new lines.

$U contains usernames.txt, $I contains fullnames to go with the -c command and $Z contains the ending numbers from 1-9 to set the -u ending value. What I hoped the script would achieve is that it would populate the useradd command with the values I outlined to make the useradd command execute adding users with their username, comment field (full name) and an ID value. I used an echo command to test what the output was before actually executing the useradd command part of the script on the system. However, the script actually interpreted the spaces in the fullnamecomment.txt as separate iterations and basically the script prints out the same query 9 times sequentially adding 1 to the -u value.

@Community, can anyone provide any input on optimizing this script or changing my method to add multiple users populating username, comment and ID values within the useradd command?

Thanks.

#!/bin/bash

for U in $(cat username.txt)
do
        for I in $(cat fullnamecomment.txt)
        do
                for Z in {1..9}
                do
                        #useradd $U -m -c $I -u 10010$Z
                        echo "useradd $U -m -c $I -u 10010$Z"
                done
        done
done

CodePudding user response:

When you nest your loops, that means you want to run the inner loop over and over, every time the outer loop happens. That's clearly not what you actually mean to do here; your code is instead intended to iterate over the variables in lockstep, pairing up each username, fullname and UID. That means you should have only one loop instead of nesting multiple loops inside each other.

offset=0
while IFS= read -r username <&3 && IFS= read -r fullname <&4 && ((   offset )); do
  useradd "$username" -m -c "$fullname" -u "$((100100   offset))"
done 3<username.txt 4<fullnamecomment.txt

CodePudding user response:

Resolution from SteveKline modified:

#!/bin/bash
Z=100101
IMPORTCSV=$(cat tabdelimituserlist.csv)
while read -r line; do
        U=$(echo $line | cut -f1 -d:)
        I=$(echo $line | cut -f2 -d:)

        echo "useradd "$U" -m -c "$I" -u "$Z""
        #useradd "$U" -m -c "$I" -u "$Z"

        #Optional since RHEL passwd permits stdin arguments
        #echo 73mp@ssw0rd | passwd --stdin "$U"

        Z=$(( $Z   1 )) #Will increment 1 after 100100 for each user added
done <<< "$IMPORTCSV"

CodePudding user response:

Looks like you're getting undesired loops?? You could do a single CSV and not iterate other users over the intended user.

Create a Tab Delimited CSV for users

User1    User1 Full Name
User2    User2 Full Name

Then you can parse with a while read...

Z=100100
importcsv=$(cat userimport.csv)
while read -r line; do
   $U=$(echo $line | cut -f1 -d$'\t')
   $I=$(echo $line | cut -f2 -d$'\t')

   useradd "$U" -m -c "$I" -u "$Z"
  
   #Optional since RHEL passwd permits stdin arguments 
   #echo 73mp@ssw0rd | passwd --stdin "$U"

   $Z=$(( $Z   1 )) #Will increment 1 after 100100 for each user added.

done <<< "$importcsv"
  • Related