简体   繁体   中英

Adding current date and time into a bash script echo command

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM