Help with script to create users from database query

#!/bin/bash
user=`mysql userList -uuserlist -puserlistpassword -s -N -e "SELECT userName FROM users WHERE activated='n'"`
for i in $user; do
useradd "$i" -m
done

This is what I have done so far. But obviously it still does not work.
I'm trying to create users based on information stored in a database. At the moment I'm just concentrating on creating users. But would like to add passwords as well. Anybody able to point me in the right direction

EDIT: I'm very new to bash scripting as well. Hence why I'm struggling.

Many thanks

Bucket

Pls post the output of the SQL query alone, i.e. what will go into the user variable.

1 Like
hell Monkey doughnut

That was done using

#!/bin/bash
users=`mysql userList -uuserlist -puserlistpassword -s -N -e "SELECT userName FROM users WHERE activated='n'"`
echo $users

Cheers

What exactly is not working? Is there any error output from your for-loop? Your code works for me.

[root@web1 scripts]# cat ./myScript
users=`mysql test -uroot -pPassXXXX -s -N -e "SELECT userName FROM users"`
for i in $users; do
  useradd "$i" -m
done

[root@web1 scripts]# ./myScript

[root@web1 scripts]# tail -3 /etc/passwd
Steve:x:500:500::/home/Steve:/bin/bash
John:x:501:501::/home/John:/bin/bash
Mike:x:502:502::/home/Mike:/bin/bash
1 Like

Your code looks OK. Pls run the script with the -vx options set and post the result.

Its OK. Thanks everyone.
I've got it working using a different script and its also adds the password to the user. :slight_smile: