我有以下脚本。当它运行时,它会将Start
和end
时间打印到文件“result.txt”中。
但是,我也想记录总运行时间(end-start
),但我echo runtine
最后所做的事情只是返回,runtime:
旁边没有记录任何内容。我做错了什么吗?
#!/bin/bash
clear
echo "Test 001" > result.txt
echo "start time: " $(date +%T) >> result.txt
start=`date +%s`
#DO STUFF HERE
end=`date +%s`
echo "end time: " $(date +%T) >> result.txt
runtime=$((end-start))
echo "runtime: " $(runtime) >> result.txt
echo " - - - "
答案1
您的 echo 语句中有一个拼写错误。它应该是
#!/bin/bash
#clear
echo "Test 001" > result.txt
echo "start time: " $(date +%T) >> result.txt
start=`date +%s`
#DO STUFF HERE
end=`date +%s`
echo "end time: " $(date +%T) >> result.txt
runtime=$((end-start))
echo "runtime: $runtime" >> result.txt
echo " - - - "
答案2
首先,你正在重新发明轮子。这就是该time
命令的用途:
$ time script.sh
real 0m0.005s
user 0m0.000s
sys 0m0.004s
然后,你会遇到语法错误:
echo "runtime: " $(runtime) >> result.txt
语法$(foo)
是命令替换,它将尝试运行foo
.你的意思是
echo "runtime: $runtime" >> result.txt
顺便说一句,您应该始终在问题中包含收到的错误消息。