-1

so far i have this.

#! /bin/bash
file=/home/userlist.txt

while read -r username directory full
do
IFS=':'
PASSWORD=$(pwgen -1 -s 16)
useradd -s /bin/bash -d $directory -c $full -m -p "$PASSWORD" "$username"
echo "$username $directory $full ---- $PASSWORD" >> /home/created
done

cannot seem to add user . the file is read. I know this because created.txt and it shows it.

jww
  • 97,681
  • 90
  • 411
  • 885
  • What shows what? Set IFS before read command, not after: `while IFS=':' -r username directory full`. Also escape all args. `useradd -s /bin/bash -d "$directory" -c "$full" -m -p "$PASSWORD" "$username"` – KamilCuk Aug 23 '18 at 09:10
  • 1
    [How to add users from data in a text file](https://unix.stackexchange.com/q/291697/56041), [Adding users from text file](https://askubuntu.com/q/739935), [Import users from CSV file](https://askubuntu.com/q/633695), [Add users in shell script with txt file](https://stackoverflow.com/q/33126405/608639), [Add new users from a text file in bash script](https://stackoverflow.com/q/15393599/608639), [How to create a user from a file in Bash?](https://stackoverflow.com/q/38285404/608639), [Create/delete users from text file using Bash script](https://stackoverflow.com/q/36449683/608639), etc. – jww Aug 23 '18 at 10:34
  • 1
    *"... [the script] cannot seem to add user..."* is not a good problem statement. Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Aug 23 '18 at 10:49
  • Welcome to SO. Have you run the script as root? – Ilia Gilmijarow Aug 23 '18 at 11:40

1 Answers1

0
    #!/bin/bash
    file=userlist.txt
    IFS=':'
    while read username directory full; do
      PASSWORD=$(pwgen -1 -s 16)
      useradd -s /bin/bash -d $directory -c $full -m -p "$PASSWORD" "$username"
      echo "$username $directory $full ---- $PASSWORD" >> created
    done < $file