3

I have a script that takes many hours to finish. I would like to add the current date and time to an echo command that prints to the screen every time a step completes.

So my command creates and inserts about 20 databases. some databases take several hours to insert so I would like to print the time each step finishes to the screen so you can know where in the process the restore is. Here is what I have so far.

#!/bin/sh for DB_File in *.sql ; do mysqladmin create ${DB_File%%-*} echo ${DB_File%%-*} has been created. date +%x_%X mysql ${DB_File%%-*} < $DB_File echo $DB_File inserted into database. date +%x_%X done 
3
  • 1
    Either put the date command on its own line or use $(date +%x_%X) to substitute the command output on the echo line. Commented Mar 16, 2015 at 15:48
  • Thanks if I use echo $(date) it works. the $(date +%x_%X) gives an error "date: invalid date ‘=%x_%X’" but I can work with the output from just date. Thank You. Commented Mar 16, 2015 at 17:15
  • Just date. No need for echo $(date) it is pointless. And =%x_%X is not the same as +%x_%X. =) Commented Mar 16, 2015 at 17:16

1 Answer 1

4

This should work:

for DB_File in *.sql ; do mysqladmin create ${DB_File%%-*} echo ${DB_File%%-*} has been created. `date "+%F %T"` mysql ${DB_File%%-*} < $DB_File echo $DB_File inserted into database. `date "+%F %T"` done 

By using backquotes the result of date command is used. If you need another date format, just see what "date --help" has on offer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.