I want to send an e-mail to users within a database.
SQL query:
SELECT nickname, email FROM db_Users;
I want the e-mail to use both columns for example:
To: %email
Hi %nickname,
Message
Can someone show me basic example to do it using a shell script? I just want the basics to know how to get started and not a fully written script :-)
Thanks
Usually I use parameter for replace variables like that. Something like this :
--top of script
NAME=$(mysql -u $USER $DBNAME -p$PWD -sN -e "SELECT nickname FROM db_Users")
EMAIL=$(mysql -u $USER $DBNAME -p$PWD -sN -e "SELECT email FROM db_Users")
echo "${NAME} Message for you" | mail -s "This is the subject" ${EMAIL}
You could do a loop fot each user, including all members in list :
I=0
for i in ${NAME[#]}
do
-- E-mail
I=`expr $I + 1`
done
Related
I am fairly new to shell scripting and struggling to get results from a sql query in form of HTML table.
I am using following code in a shell script to fetch data:
echo "Subject: Test HTML e-mail" > html.txt
echo "Content-Type: text/html" >> html.txt
sqlplus -s -m "HTML ON TABLE 'BORDER="2"'" User/Pass >> html.txt
SELECT * from EMP;
EXIT;
EOF
and then I am sending html.txt using sendmail
I want to know how I can add more formatting options to this code. Right now its just sending the contents of EMP table in a predefined formatting.
Do I need a CSS template for this? If yes, could anyone show it with a small example?
Thanks
There's plenty of similar questions out there that seem to answer this, but it's not working for me.
Here's mysql query in a bash script, the resultant row comes back as a string, not as an array.
Am I missing something?
while read -a row;
do
echo "${row[0]}";
done < <(mysql -u $dbuser -p$dbpass -N --database=$dbname --host=$dbhost --batch -se "SELECT id, CONCAT(id, '_', filename) from photos" );
}
This echoes 200 200_filename.jpg.
I would think it is supposed to echo 200.
Echoing ${row[1]} displays a blank line.
And the magic spell is the IFS. Adding that in with the tab fixes the array issue
while IFS=$'\t' read -a row;
do
echo "${row[0]}";
done
My script is designed to cycle through environments, hit a database and check to see if any first/last name combination has an associated user ID. I am not sure why I'm getting the error listed below.
#/bin/bash
#specify user to search for *******DO NOT PUT SPACE, ie. jaylefler
echo "Please enter user first name: "
read first_name
echo "Please enter user last name: "
read last_name
echo "Please enter LDAP User ID: "
read ldapuser
echo "Please enter LDAP password: "
stty -echo
read ldappw
stty echo
#logs to write output to
log="ui_${username}_access.log"
finallog="${username}_ui_access.txt"
#create file if not exist, else null it
[[ -f ${log} ]] && cat /dev/null > ${log} || touch ${log}
[[ -f ${finallog} ]] && cat /dev/null > ${finallog} || touch ${log}
#log it all
{
echo "environment"
sshpass -p $ldappw ssh $ldapuser#54.123.777.567 'mysql -h host -u user - ppassword database -e \
"select user_id from users where first like "%'${first_name}'%" and last like "%'${last_name}'%";"'
} > $log
When I execute the script, I receive the following errors:
Please enter user first name:
jay
Please enter user last name:
lefler
Please enter LDAP User ID:
jlefler
Please enter LDAP password:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%jay% and last like %lefler%' at line 1
jlefler#ubuntu:~/Desktop$
I'm not that familiar with this type of scripting, but it would seem to me that around %jay% and %lefler% there would need to be quotations so that the SQL appears as:
select user_id from users where first like '%jay%' and last like '%lefler%';
The primary problem in your code is that the quotation marks are inside of the percent signs. :)
The second problem is that you have created something that is vulnerable to SQL injection. I have absolutely no idea how to create bound parameters from a Bash script, but you need to figure that out!
The query to MySQL is supposed to output something like this:
SELECT * FROM pet WHERE name LIKE '%w%';
In bash one of the way to concatenate is as follow:
mystring="some ${string1} arbitrary ${string2} text"
EDIT:
So combining all Plus making somethin to expand the strings inside ''. The problem is that shell Can not expand strings between single quotations marks.
Something like this should work:
CommandFinale=$($sqlCommand -e "select user_id from users where first like '%${first_name}%' and last like '%${last_name}%';")
Then use the variable CommandFinale in you connection chain.
I have not bee able to test it but it should be like that or very similar.
My script cycles through multiple environments and if a user is found, it prints out their ID. The desired output is as follows:
environment1
<user_id>
However, my script is currently working to print out every environment even if there isn't a user id, like follows:
environment1
<user_id>
environment2
environment3
environment4
<user_id>
I would like the code to print out "NO USER FOUND" if the user_id does not exist, rather than excluding the environment altogether.
The code below is what is being utilized:
#log it all
{
echo "environment"
sshpass -p $ldappw ssh $ldapuser#12.345.67.89 'mysql --skip-column-names -hhost -u user -ppassword database -e \
"select user_id from users where first like '"'%${first_name}%' and last like '%${last_name}%';"'"'
} > $log
Any help would be much appreciated for this issue. Unfortunately I'm just beginning to learn more advanced MySQL and Linux command line tools and am not quite proficient enough to know how to handle this problem.
may be
select ifnull(user_id,'NO USER FOUND') from users where first like '"'%${first_name}%' and last like '%${last_name}%';
I can query our MySQL database to get the email address of all registered user accounts. I want to send an email to every user, one by one.
Query
SELECT email FROM users
Output
email
-------
email#domain.com
email#domain.com
email#domain.com
Email
cat <<EOF | sendmail -t
To: %%u.email%%
Subject: Testing
From: sender#example.com
This is a test message
EOF
How can I use the email addresses returned from the MySQL database instead of %%u.email%%?
Giving you have defined sendmail and query scripts:
while read -r email; do ./sendmail "${email}"; done < <(./query | head -n +2)