0

The last piece of my script is only pending.

kindly refer to the code below.

master_db_user='root'
master_db_passwd='123'
master_db_port='3306'
master_db_host='localhost'
master_db_name='uppcldashboard'

Now=$(date +"%d-%m-20%Y")



#Preparing script 

#SQL_Query='select * from test_table;'

#MySql Command to connect to a database 

mysql -u$master_db_user -p$master_db_passwd -D$master_db_name -e  'select * from uppcl_amount_details into outfile "/created_files/uppcl.csv" fields terminated by "," lines terminated by "\n"';

mv /created_files/uppcl.csv /created_files/offline_collection$Now.csv

sed -i "1i ID","Cashier_id","Discom","Division_Code","division_Name" /created_files/offline_collection$Now.csv
echo "End of the Script"

okay the main question is this.. so I have column in my table called posting_dates. because of posting date we are able to know that the data present is the table is of that particular date.

now the issue within my script is that i can't use any variable which chages in the query. it simply doesn't work as it takes the query as a statement.

so basically what i want is this

i to make such a query in which i don't need to mention any date. i want it to fectch data of that particular day. because i can't change the query every single time.

I can't be like where posting_date="2020-03-30", I need something that automatically makes changes in the query everyday. because it is gonna be used to make file of that particular date.

Really need Help on this people.

Thanks and Regards, Sagar Mandal

  • Use something like CURDATE in your query? – Freddy Apr 14 '20 at 14:29
  • @Freddy bro like how...just give me hint bro...thats all –  Apr 14 '20 at 14:30
  • 1
    Replace the date string with CURDATE(), that's all. – Freddy Apr 14 '20 at 14:50
  • I don't understand the problem. You already know how to get today's date into a variable, you're doing it in your script. Why don't you just use $Now in your SQL query? Something like where posting_date="$Now". Why won't that work? – terdon Apr 14 '20 at 14:55
  • @terdon It's not picking up $Now because my query is in single quotes. –  Apr 14 '20 at 15:07
  • @Freddy I think you might have helped me. Thanks buddy just gonna use CURDATE() in my where clause. –  Apr 14 '20 at 15:08
  • @Freddy you just completed the last piece of my script....Thank you very much. –  Apr 14 '20 at 17:46
  • @pLumo thanks buddy for also looking into my stuff :-). i appreciate your help. thank you –  Apr 14 '20 at 17:51

1 Answers1

1

Just don't use single quotes:

mysql -u$master_db_user -p$master_db_passwd -D$master_db_name \
    -e "select * from uppcl_amount_details where date='$Now' into \
         outfile '/created_files/uppcl.csv' fields terminated by ',' \
          lines terminated by '\n'";

That way, you can also get the file name right from the beginning:

into outfile '/created_files/offline_collection$Now.csv'

Also note that your date format is probably not what you want. You have this:

$ date +"%d-%m-20%Y"
14-04-202020

But you probably want this:

$ date +%F
2020-04-14

So: Now=$(date +%F).

terdon
  • 242,166
  • Buddy, you taught me something and yes your method is also absolutely right. thank you so much for replying so quick. thanks alot man. both of you –  Apr 14 '20 at 17:48