bash 简单的计时器来测量操作完成所需的秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3840558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple timer to measure seconds an operation took to complete
提问by Michal M
I run my own script to dump databases into files on a nightly basis.
I wanted to count time (in seconds) it takes to dump each database, so I was trying to write some functions to help me achieve it, but I'm running into problems.
我每晚都运行自己的脚本将数据库转储到文件中。
我想计算转储每个数据库所需的时间(以秒为单位),所以我试图编写一些函数来帮助我实现它,但我遇到了问题。
I am no expert in scripting in bash, so if I'm doing it plain wrong, just say so and ideally suggest alternative, please.
我不是 bash 脚本方面的专家,所以如果我做错了,请直说,最好建议替代方案。
Here's the script:
这是脚本:
#!/bin/bash
declare -i time_start
function get_timestamp {
declare -i time_curr=`date -j -f "%a %b %d %T %Z %Y" "\`date\`" "+%s"`
echo "get_timestamp:" $time_curr
return $time_curr
}
function timer_start {
get_timestamp
time_start=$?
echo "timer_start:" $time_start
}
function timer_stop {
get_timestamp
declare -i time_curr=$?
echo "timer_stop:" $time_curr
declare -i time_diff=$time_curr-$time_start
return $time_diff
}
timer_start
sleep 3
timer_stop
echo $?
The code should really be quite self-explanatory. echocommands are only for debugging.
I expect the output to be something like this:
代码应该是不言自明的。echo命令仅用于调试。
我希望输出是这样的:
$ bash timer.sh
get_timestamp: 1285945972
timer_start: 1285945972
get_timestamp: 1285945975
timer_stop: 1285945975
3
Now this is not the case unfortunately. What I get is:
不幸的是,现在情况并非如此。我得到的是:
$ bash timer.sh
get_timestamp: 1285945972
timer_start: 116
get_timestamp: 1285945975
timer_stop: 119
3
As you can see, the value that local var time_currgets from the command is a valid timestamp, but returning this value causes it to be changed to an integer between 0 and 255.
如您所见,local vartime_curr从命令中获取的值是一个有效的时间戳,但返回此值会导致它更改为 0 到 255 之间的整数。
Can someone please explain to me why this is happening?
有人可以向我解释为什么会这样吗?
PS. This obviously is just my timer test script without any other logic.
附注。这显然只是我的计时器测试脚本,没有任何其他逻辑。
UPDATEJust to be perfectly clear, I want this to be part of a bash script very similar to this one, where I want to measure each loop cycle.
更新为了完全清楚,我希望它成为与此非常相似的 bash 脚本的一部分,我想在其中测量每个循环周期。
Unless of course I can do it with time, then please suggest a solution.
除非我当然可以使用time,否则请提出解决方案。
采纳答案by mob
$?is used to hold the exit status of a command and can only hold a value between 0 and 255. If you pass an exit code outside this range (say, in a C program calling exit(-1)), the shell will still receive a value in that range and set $?accordingly.
$?用于保存命令的退出状态,只能保存 0 到 255 之间的值。如果传递超出此范围的退出代码(例如,在 C 程序调用中exit(-1)),shell 仍将收到该范围内的值并进行相应设置$?。
As a workaround, you could just set a different value in your bash function:
作为一种解决方法,您可以在 bash 函数中设置不同的值:
function get_timestamp {
declare -i time_curr=`date -j -f "%a %b %d %T %Z %Y" "\`date\`" "+%s"`
echo "get_timestamp:" $time_curr
get_timestamp_return_value=$time_curr
}
function timer_start {
get_timestamp
#time_start=$?
time_start=$get_timestamp_return_value
echo "timer_start:" $time_start
}
...
回答by Alex Howansky
You don't need to do all this. Just run time <yourscript>in the shell.
你不需要做这一切。只需time <yourscript>在shell中运行即可。
回答by Daenyth
This is happening because return codes need to be between 0-255. You can't return an arbitrary number. If you continue to refuse to use the builtin timefunction and roll your own, change your functions to echo their stamp and use a process expansion ($()) to grab the value.
发生这种情况是因为返回代码需要在 0-255 之间。您不能返回任意数字。如果您继续拒绝使用内置time函数并推出自己的函数,请更改您的函数以回显它们的标记并使用进程扩展 ( $()) 来获取值。
回答by Rohith
I believe you should be able to use the existing "time" function.
我相信您应该能够使用现有的“时间”功能。
After Update to the question: This was the bit of script from your link which was doing a for loop.
更新问题后:这是您的链接中的一小段脚本,它正在执行 for 循环。
# dump each database in turn
for db in $databases; do
echo $db
$MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD
--databases $db > "$OUTPUTDIR/$db.bak"
done
You could extract the inner portion of the loop into a new script (call it dump_one_db.sh)
and do this inside the loop:
您可以将循环的内部部分提取到一个新脚本中(调用它dump_one_db.sh)并在循环内执行此操作:
# dump each database in turn
for db in $databases; do
time dump_one_db.sh $db
done
Make sure to write the output of the time against the db name into some file.
确保将时间的输出与数据库名称写入某个文件。

