【问题标题】:C shell doing arithmetic operations with large numbersC shell 对大数进行算术运算
【发布时间】:2012-10-11 23:31:28
【问题描述】:

首先:对不起使用c shell,怪我公司而不是我。我和你们大多数人现在一样讨厌这该死的东西(起初我想,嘿,这还不错)。

我正在尝试减去从时间戳中获得的大量数字。这是我正在尝试的:

set curTime = `date +%s%N`
#... some stuff
@curTime = `date +%s%N` - $curTime #get the diff
echo "time taken: $curTime"

但是我猜这些数字太大了——在我尝试几秒钟之前它工作得很好。这是我在日志中看到的:

@curMilli = 1349996279792995000 - 1349996279170458000
@curMilli: Command not found.

正如我所说,我对date +%s 做了完全相同的事情,这很好,所以我假设这与数字的庞大有关。

我该怎么做?非常感谢。

【问题讨论】:

  • 文章en.wikipedia.org/wiki/Bc_programming_language有一小节“在shell脚本中使用bc”。
  • 谢谢。我可以在修改命令方面获得一些帮助吗?我试过@curMilli = 1349996279792995000 - 1349996279170458000 | bc 没有成功;我还尝试了用括号 () 包围表达式部分的变体,但无济于事。
  • 我认为你需要一个分号。 echo "1349996279792995000 - 1349996279170458000;" | bc622537000
  • 太棒了!而且,如果我将这些横向刻度线包裹起来,它似乎可以在变量赋值中工作。继续发帖并回答以获得被接受的兄弟

标签: shell unix csh


【解决方案1】:

文章http://en.wikipedia.org/wiki/Bc_programming_language 有一小节“在shell 脚本中使用bc”。一个测试:

set curTime = `/bin/date +%s%N`
/bin/sleep 2
set prevTime = $curTime
set curTime = `/bin/date +%s%N`
set diff = `echo "$curTime - $prevTime;" | /usr/bin/bc`
echo $diff

将给出(初始20变量后面的数字):

2016204108

P.s:我希望我能为"I hate the damn thing as much as most of you do now (at first I was like, hey this ain't so bad)."投两次票

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2021-12-27
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多