Creating a group of users with script

Hi,

I have a file with usernames, and the comment section, e.g :

Data removed by request of sanchitadutta91, 20 May 2020

I need to add these users into a server. Is it possible to use a script to create the users, together with the comment ?

From the commandline to add one user, the command will be :

useradd -m user -c "comment"

How do I do the same, for multiple users above, using script(automation)?

Yes, it is possible.

The simplest way, I would say, is to use a while loop with a read, setting the field separator (IFS) to : to get each field from the file, and inside the loop run the useradd command.

1 Like

Different approach:

sed 's/^/echo useradd -m /; s/;/,/; s/:/ -c \\"/; s/$/\\"/' file

redirect into a file and run / source that, or pipe the result into a shell. Remove the echo if happy with the proposal. Unfortunately, that sole ; in the comment will spoil the shell input and thus must be removed.

1 Like