I’ve tried for hours to create bulk users from a text file but I cant make it work. In the text file i have the following format:
John Smith:Student
The code:
#!/bin/bash
FILE=/home/knoppix/users.txt
GECOS=$(cat $FILE | cut -d: -f1)
USRGRP=$(cat $FILE | cut -d: -f2)
groupadd -f $USRGRP
echo "type username"
read USERNM
USERPW="123456"
useradd $USERNM -p $USERPW -g $USRGRP -c "$GECOS,$USRGRP" -d $HOME/$USERNM -s $USERSH -m
It is not working, when I tried to debug the bash script I think its not reading line by line, its grabbing all the content in the fields.
I’m missing a do while or while IFS or for line in users.txt. Something like this but I’m not expert help. also should I use newusers ir just useradd.
This assumes users.txt values are separated by spaces chars and that there are 4 words per line that you want to do some processing on.
If you values are separated with something beside space chars, for example the ‘:’ char, then you have to tell the read command what to use as in IFS (InternalFieldSep), so
Also,
Code that is formed like
can be reduced to
Also note that for each line in $FILE, you will get a value, so if there are 3 lines of text, ECOS will be
ECOS="line1Val line2Val line3Val"I hope this helps.