Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-30-2004, 08:32 PM   #1
LQ Newbie
Registered: Apr 2003
Posts: 27

Rep: Reputation: 15
MySQL in a shell script?!

Hey guys!
Im in charge to code a shell script that checks a mySQL DB for email adresses and sends a predefined email to the ones returned...
Sooo ... is it even possible to have SELECTs in a shell script? And how can I send "real emails" (i mean, through a SMTP server) to those ppl?

Would be great if someone could help me out
Huge thanks in advance,
Old 05-31-2004, 06:47 AM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Sure that's possible. Assuming you have a .my.cnf in the home directory of the user that will execute the script. This to make sure the script won't ask for a password, and also there's no password inside the script. Also make sure the file has the proper rights so only root and the user can read the script:
shell$ ls -l ~/.my.cnf
-rw------- 1 zaubara zaubara    41 Nov 12 2003 /home/zaubara/.my.cnf

shell$ cat ~/.my.cnf
Now you can make a shell-script like this:
QUERY="select email from mailtable"
echo "$QUERY" | mysql yourdatabase | while read address ; do
    cat filewithmessage.txt | mailx -s "some subject" "$address"
- For sending mail this way, you need to have a MTA (mailserver, e.g. sendmail) running on the system
- If you don't have the "mailx" program, try "mail".

Last edited by Hko; 05-31-2004 at 06:50 AM.
Old 05-31-2004, 06:27 PM   #3
LQ Newbie
Registered: Apr 2003
Posts: 27

Original Poster
Rep: Reputation: 15
Wow, great! You really helped me out thanks!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
PHP Shell Script (with MySql) Tony Empire Programming 1 09-20-2005 10:59 AM
MySQL connection in shell script - how to? philipz Programming 3 04-29-2004 08:04 PM
Import records to MySQL Database from a shell script chrisk5527 Linux - General 12 03-24-2004 10:49 PM
How get a mysql shell response in a script. philipina Programming 3 03-16-2004 12:51 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration