I have written this piece of code to insert rows into table if not there, else, update the 'cnt' by one more. This works very well, when I invoke it from command line, when I invoke it from shell script, this does not work.
file=$1
echo "$0: Path to store $file" ;
res=`mysql -e "use spam_sending_scripts; select * from spamemailcount where path = '$file' limit 0,1"`
echo $res
if [ -z "$res" ]
then
mysql -e "use spam_sending_scripts; INSERT INTO spamemailcount (cnt,path) VALUES(1,'$file');"
echo "Inserting into DB $file , res $res" ;
exit ;
fi
mysql -e "use spam_sending_scripts; update spamemailcount SET cnt=cnt+1 where path = \"$file\"" ;
echo "Updating into DB $file" ;
#mysql -e "use spam_sending_scripts; select * from spamemailcount" >> /var/log/sendmail.log
mysql -e "use spam_sending_scripts; select * from spamemailcount"
root@server [/home]# insertintodb.sh AAA ==> This is working fine.
When I invoke from the other script, this file is executed, But Insert does not work.
I invoke it like: /home/insertintodb.sh $path
The $path variable is getting passed to insertintodb.sh correctly.
I am getting the following Error:
++ mysql -e 'use spam_sending_scripts; select * from spamemailcount where path = '\''hackerssquadron.com/wp-comments-post.php'\'' limit 0,1'
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
/home/insertintodb.sh "$path"?bash -x /home/insertintodb.sh "$path"~/.my.cnfwith[mysql]configuration for username and password etc, if you don't have default login/server in the global config.