LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 06-21-2018, 04:11 PM   #1
albatros.78
LQ Newbie
 
Registered: Jun 2018
Posts: 3

Rep: Reputation: Disabled
Unhappy execute SQL Query at linux boot return empty value


Hello to all,
I would be grateful if anyone knows how to solve a problem by running a sql query through a scritpt at system startup.

The script is written in bash as follows:


#!/bin/bash

query_result=$(mysql -NB -u dbuser -p'dbpwd' -DDATABASE -e "SELECT name FROM users ORDER BY id;")
echo "Result: "$query_result >/tmp/ppp.txt


If I run the script manually (typing ./script.sh), the query returns the list of data in the database.
If performed automatically during startup (adding a line in /etc/rc.local file) , it only return "Result:" (the string made by echo command in the script...)

Some idea ?
thanks
 
Old 06-21-2018, 05:37 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Try using an absolute path to mysql. It’s likely that the script doesn’t know where it is when run from the init file..
 
Old 06-21-2018, 06:03 PM   #3
albatros.78
LQ Newbie
 
Registered: Jun 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Try using an absolute path to mysql. It’s likely that the script doesn’t know where it is when run from the init file..
Thank you,
I tried to write the script in the following way:

but the problem persists

#!/bin/bash

query_result=$(/usr/bin/mysql -NB -u dbuser -p'dbpwd' -DDATABASE -e "SELECT name FROM users ORDER BY id;")
echo "Result: "$query_result >/tmp/ppp.txt
 
Old 06-21-2018, 07:07 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Hmmm.
Is DATABASE an obfuscation for your post, or is that a variable that exists in your CLI environment?
If the latter, it's not present in the environment when run from the init file...either populate within the script, or use the actual database name there.

Oh. Please put code and output examples in [code] tags...
Code:
#!/bin/bash

query_result=$(/usr/bin/mysql -NB -u dbuser -p'dbpwd' -DDATABASE -e "SELECT name FROM users ORDER BY id;")
echo "Result: "$query_result >/tmp/ppp.txt
PS You're sure that mysql is at /usr/bin/mysql? Mine is at /bin/mysql

Last edited by scasey; 06-21-2018 at 07:14 PM.
 
Old 06-21-2018, 11:36 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You might want to start mysqld beforehand.
 
1 members found this post helpful.
Old 06-22-2018, 01:37 AM   #6
albatros.78
LQ Newbie
 
Registered: Jun 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
SOLVED

Quote:
Originally Posted by NevemTeve View Post
You might want to start mysqld beforehand.
Yeah!!!
Adding this command "/etc/init.d/mysql start" before the script as:

/etc/init.d/mysql start
query_result_b=$(/usr/bin/mysql -NB -u root -p'root' -DAUTOCONTROLLER -e "SELECT dev_mac_address FROM bt_devices_enabled ORDER BY id;")
echo "Dopo :"$query_result_b >/tmp/ppp.txt


Worked!

Thank very much!!!
 
  


Reply



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
How to return empty char * ? cpthk Programming 16 08-28-2009 11:17 PM
MYSQL: SQL query takes 10 minutes to execute. Need help optimizing. robel Programming 7 10-29-2008 04:44 PM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
ColdFusion - Using a script to build and execute SQL query when a button is clicked Locura Programming 1 02-25-2004 09:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 PM.

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