打击时间不对

时间:2014-12-07 01:09:20

标签: bash shell time vagrant provisioning

我目前正在使用shell脚本配置Vagrant盒子,我想让人们知道在配置完成之前他们还剩多少时间。但是在运行脚本时,剩余时间与通过的时间不匹配。我已经在下面的代码输出中表示了差异。有什么问题?谢谢!

我的脚本运行如下

  1. 从Unix纪元获取当前时间并设置剩余时间start=$(date +%s) ; remaining=1200
  2. 打印剩余时间echo start=$(date +%s)
  3. 回显当前步骤并运行步骤的脚本echo "Moving .ssh" ; ./move_ssh.sh
  4. 通过减去新时间并添加开始时间来重新计算剩余时间

    一个。这与从纪元获得开始时间和当前时间之间的差异并从剩余的前一时间减去它相同。 (a - (b - c)= a - b + c)

    remaining=`expr $remaining - $(date +%s) + $start`
    
  5. 提供脚本

    start=$(date +%s) ; remaining=1200
    echo "Time Remaing: $remaining ($(date "+%T"))"
    echo "Moving .ssh" ; ./move_ssh.sh
    ...
    remaining=`expr $remaining - $(date +%s) + $start` ; echo "Time Remaing: $remaining ($(date "+%T"))"
    ...
    

    流浪输出

    ==> default: Time Remaing: 1200 (00:43:58)
    ==> default: Moving .ssh
    ==> default: Time Remaing: 1200 (00:43:58)
    ==> default: Installing git
    ==> default: Time Remaing: 1190 (00:44:08)
    ==> default: Installing mongodb
    ==> default: Time Remaing: 1140 (00:44:48) (1190 to 1140 is 50 but 44:08 to 44:48 is 40)
    ==> default: Installing redis-server
    ==> default: Time Remaing: 1082 (00:44:56) (1140 to 1082 is 68 but 44:48 to 44:56 is 8)
    ==> default: Installing python-pip
    ==> default: Time Remaing: 1001 (00:45:19) (1082 to 1001 is 81 but 44:56 to 45:19 is 23)
    ==> default: Installing python-dev
    ==> default: Time Remaing: 887 (00:45:52) (1001 to 887 is 114 but 45:19 to 45:52 is 33)
    ==> default: Installing mysql-server-5.5
    ==> default: Time Remaing: 713 (00:46:52) (887 to 713 is 174 but 45:52 to 46:52 is 60)
    

1 个答案:

答案 0 :(得分:1)

以下是我创建另一个变量$_remaining的预期行为:

start=$(date +%s) ; remaining=1200
_remaining=$( expr $remaining - $(date +%s) + $start ) ; echo "Time Remaing: $_remaining ($(date "+%T"))"

使用原始set -x调试显示问题:

bob@crunchbang:~/documents/scraping/txt$ set -x
bob@crunchbang:~/documents/scraping/txt$ start=$(date +%s) ; remaining=1200
++ date +%s
+ start=1417917612
+ remaining=1200
bob@crunchbang:~/documents/scraping/txt$ remaining=$( expr $remaining - $(date +%s) + $start ) ; echo "Time Remaing: $remaining ($(date "+%T"))"
+++ date +%s
++ expr 1200 - 1417917626 + 1417917612
+ remaining=1186
++ date +%T
+ echo 'Time Remaing: 1186 (21:00:26)'
Time Remaing: 1186 (21:00:26)
bob@crunchbang:~/documents/scraping/txt$ remaining=$( expr $remaining - $(date +%s) + $start ) ; echo "Time Remaing: $remaining ($(date "+%T"))"
+++ date +%s
++ expr 1186 - 1417917634 + 1417917612
+ remaining=1164
++ date +%T
+ echo 'Time Remaing: 1164 (21:00:34)'
Time Remaing: 1164 (21:00:34)
bob@crunchbang:~/documents/scraping/txt$ remaining=$( expr $remaining - $(date +%s) + $start ) ; echo "Time Remaing: $remaining ($(date "+%T"))"
+++ date +%s
++ expr 1164 - 1417917652 + 1417917612
+ remaining=1124
++ date +%T
+ echo 'Time Remaing: 1124 (21:00:52)'
Time Remaing: 1124 (21:00:52)
bob@crunchbang:~/documents/scraping/txt$ remaining=$( expr $remaining - $(date +%s) + $start ) ; echo "Time Remaing: $remaining ($(date "+%T"))"
+++ date +%s
++ expr 1124 - 1417917656 + 1417917612
+ remaining=1080
++ date +%T
+ echo 'Time Remaing: 1080 (21:00:57)'
Time Remaing: 1080 (21:00:57)

您可以按set +x

关闭调试