简体   繁体   English

将当前日期和时间添加到bash脚本echo命令中

[英]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. 我想将当前日期和时间添加到每次完成步骤时都会显示到屏幕上的echo命令。

So my command creates and inserts about 20 databases. 因此,我的命令创建并插入了大约20个数据库。 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. 通过使用反引号,可以使用date命令的结果 If you need another date format, just see what "date --help" has on offer. 如果您需要其他日期格式,请查看提供的“ date --help”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 bash脚本查找文件名中的日期比当前日期和时间新的所有文件 - bash script to find all files where date in filename is newer than current date and time bash脚本执行日期/时间 - bash script execution date/time 在bash脚本中跟踪执行时间,如果花费太长时间则终止当前命令 - Keep track of execution time in a bash script and terminate current command if it takes too long bash脚本修改以从echo命令获取参数 - bash script modify to take parameter from echo command 如何让bash date脚本返回相对于非当前时间的星期几? - How do I get the bash date script to return a day of the week relative to a non-current time? 日期命令作为 bash 脚本中的变量。 需要每次调用而不是在变量声明期间调用 - Date command as a variable in a bash script. Needs to be invoked each time instead of during variable declaration 使用 date 命令在 bash 脚本中的 sed 命令中重新格式化日期 - Use date command to reformat date within sed command in bash script bash中的echo命令行为问题 - echo command behaviour issue in bash BASH:grep 在 shell 脚本中不起作用,但 echo 显示正确的命令并且它在命令行上起作用 - BASH: grep doesn't work in shell script but echo shows correct command and it works on command line BASH日期命令:无法重新组合日期和时间 - BASH date command: can't re-combine date and time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM