Help with script to create users from database query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script to create users from database query
# 1  
Old 12-15-2012
Help with script to create users from database query

Code:
#!/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
# 2  
Old 12-16-2012
Pls post the output of the SQL query alone, i.e. what will go into the user variable.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-16-2012
Quote:
Originally Posted by RudiC
Pls post the output of the SQL query alone, i.e. what will go into the user variable.
Code:
hell Monkey doughnut

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

Cheers
# 4  
Old 12-16-2012
What exactly is not working? Is there any error output from your for-loop? Your code works for me.

Code:
[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

This User Gave Thanks to Scott For This Post:
# 5  
Old 12-16-2012
Your code looks OK. Pls run the script with the -vx options set and post the result.
# 6  
Old 12-16-2012
Its OK. Thanks everyone.
I've got it working using a different script and its also adds the password to the user. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create users from a text file by script

Hi All, I am new to scripting, i want to create one script in which i can add users from a text file, assign them a default password like 123456. It should be like: Username= $i (it should take users from text file one by one) Password : 123456(default for every user) (1 Reply)
Discussion started by: prad_rocxx
1 Replies

2. UNIX and Linux Applications

Really simple shell script to create oracle database

Hello , I am new in this forum and need your help as I am totally confused :confused: I read a lot of threads and tried to search a lot but did not get the exact answer to my question. I just want a simple (content wise may be long) shell script to create oracle database. In detail:... (5 Replies)
Discussion started by: rahoolm
5 Replies

3. Programming

Creating a bash script that create/open database

Hi. I have two text files(tables) which include some information and I want to make some query codes using them. First of all, I want to create bash script that read this two tables, create/open database and insert data from files into database. #!/bin/bash while read line; do ... (1 Reply)
Discussion started by: rlaxodus
1 Replies

4. Shell Programming and Scripting

Korn Script to connect and query oracle database

I've been sent the following script to finish. It's supposed to connect to an oracle database, query it, and send an email if the query result value is one or more. Currently it isn't connecting properly, just giving the following error: ERROR: ORA-01017: invalid username/password; logon denied... (2 Replies)
Discussion started by: jackmorgan2007
2 Replies

5. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

6. Shell Programming and Scripting

Create new users in DMZ box using script

I remote to many DMZ boxes every day to run batch file that allows me to create users. I create users in 17 DMZ boxes every day which takes a lot of my time. Is there any script that would do this job from my local computer? Thank you for your help! (3 Replies)
Discussion started by: idiazza
3 Replies

7. Shell Programming and Scripting

Create mysql database with bash script - confused

Hi, i have the following: db="create database xxx;GRANT ALL PRIVILEGES ON xxx.* TO user@localhost IDENTIFIED BY 'password';FLUSH PRIVILEGES;quit;" mysql -u root -p$mysql_pass -e "$db" I don't understand why this is failing, it works fine when run from cmd but when is run in a bash script,... (1 Reply)
Discussion started by: ktm
1 Replies

8. Shell Programming and Scripting

Tables to query to find users for database from shell script

I am coding shell script. I need to connect to different databases like DB2, Oracle and Sybase. I would then need to query tables where it has all the groups, users for that database. I would also need who has what kind of permissions. EG: I know for DB2 some TABAUTH table needs to be... (0 Replies)
Discussion started by: pinnacle
0 Replies

9. OS X (Apple)

interactive shell script to create users 10.4

Hello everyone, Not sure if this is the right place, but OS X isn't your standard Unix, so I figured here would be best. I am looking at creating a script that will be interactive that admins can run to create users. Now, 10.4 uses netinfo database and netinfo manager to handle it's users. ... (3 Replies)
Discussion started by: tlarkin
3 Replies

10. Shell Programming and Scripting

script to create users on many servers

Hi all, working on script to create a user acct on all our servers. for i in `cat $host_file`; do ssh $i /usr/bin/sudo /usr/bin/mkuser id='bpadm' gecos='NetBackup Admin' 2>&1 >> $log done error i get is: 3004-692 Error changing "id" to "bpadm" : Value is invalid. I have tried this in... (1 Reply)
Discussion started by: dnidiffer
1 Replies
Login or Register to Ask a Question