Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding current date and time into a bash script echo command

Tags:

linux

bash

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
like image 443
Rick Bonney Avatar asked Jul 20 '26 07:07

Rick Bonney


1 Answers

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.

like image 89
Würgspaß Avatar answered Jul 22 '26 00:07

Würgspaß



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!