ChrisBlp
ChrisBlp

Reputation: 83

Add users using useradd with a textfile

I want to create some users using a file text in which there are informations about the users. The file text is written like that :

#FIRSTNAME LASTNAME UID
firstname1 lastname1 uid1
firstname2 lastname2 uid2
etc ...

This is what I tried to do for now :

#!/bin/bash
while read var1 var2 var3
do
 sudo useradd $var1$var2 -u $var3
done < ~/Users.txt

But I have to find a way to ignore the first line... I don't know where to add grep -v "#" in my script...

(I'm trying to find the easiest way to do it)

Upvotes: 1

Views: 548

Answers (2)

sat
sat

Reputation: 14949

You can use like this:

while read line
do
     # do process
     sudo useradd "$var1$var2" -u "$var3"
done < <(grep -v '^#' file)

It is always recommended to quote the variables.

Upvotes: 1

Farhad Farahi
Farhad Farahi

Reputation: 39397

#!/bin/bash
while read var1 var2 var3
do
   if [[ ! $var1 =~ "#"* ]]
   then
       sudo useradd $var1$var2 -u $var3
   fi
done < ~/names

Upvotes: 1

Related Questions