3

I have the following script, it reads from users.txt the first and second fields and uses them to generate the username and password and creates the accounts for each line. the problem is that the script is only creating accounts for the first 2 lines and not for the rest

enter image description here

#!/bin/bash
FILE=/home/knoppix/users.txt
USERSH=/bin/bash


while IFS=":" read GECOS USRGRP ; do

groupadd -f $USRGRP



USERNM="u$(cat /dev/urandom| tr -dc '0-9' | fold -w 6| head -n 1)"
USERPW=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 6| head -n 1)


useradd $USERNM -p $USERPW -g $USRGRP -c "$GECOS,$USRGRP" -d $HOME/$USERNM -s $USERSH -m

ACCNT=$(grep $USRNM /etc/passwd)
echo "${tgrn}Account creation successful!${tr}"
echo "$ACCNT"
echo "Credentials"
echo "${tred}Username:${tr} $USERNM ${tred}Password:${tr} $USERPW"
echo
done < $FILE
Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
  • Time for a new book, _Why Jo:hnny can't have a Unix account_. (Obligatory [XKCD](http://xkcd.com/327/) reference here.) – sarnold Nov 18 '11 at 01:37
  • Put this before your `groupadd` command and post the output: `echo "[$GECOS] [$USRGRP]"; continue` – rob mayoff Nov 18 '11 at 01:50
  • root@Microknoppix:/home/knoppix# ./accounts [Jose David Garcia] [Student] [Yasin Ahmed] [Student] [Utah King] [Student] [Michael Jackson] [Student] is not even reading the last line (stack overflow:staff –  Nov 18 '11 at 01:54
  • 1
    Sounds like there's something fishy about the file. – rob mayoff Nov 18 '11 at 02:05
  • Thats right!, i changed the ownergroup and ownership of the file to root and now it operates smoothly, it creates all accounts. how did u know there was something fishy about the file –  Nov 18 '11 at 02:18
  • The script looked fine so the next thing to verify was the input to the script. – rob mayoff Nov 18 '11 at 03:12
  • 1
    If you're going to accept an answer please don't destroy your question. It makes the answer meaningless. – BoltClock Nov 18 '11 at 05:35

2 Answers2

3
#!/bin/bash

while IFS=: read GECOS USRGRP; do

    # your groupadd and useradd commands here

done < /home/knoppix/users.txt
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Hi sorry the script above was not updated, im using while IFS. now the problem is that it only adds the first two accounts from the text file –  Nov 18 '11 at 01:34
  • 2
    You need to post your actual script if you want us to debug it. – rob mayoff Nov 18 '11 at 01:35
0
#!/bin/bash
for line in $file; do
# make the account

done
rm $file
touch $file