How can I do division with variables in a Linux shell?

前端 未结 6 1058
南旧
南旧 2020-12-22 20:38

When I run commands in my shell as below, it returns an expr: non-integer argument error. Can someone please explain this to me?

$ x=20
$ y=5
$          


        
6条回答
  •  悲&欢浪女
    2020-12-22 20:54

    Why not use let; I find it much easier. Here's an example you may find useful:

    start=`date +%s`
    # ... do something that takes a while ...
    sleep 71
    
    end=`date +%s`
    let deltatime=end-start
    let hours=deltatime/3600
    let minutes=(deltatime/60)%60
    let seconds=deltatime%60
    printf "Time spent: %d:%02d:%02d\n" $hours $minutes $seconds
    

    Another simple example - calculate number of days since 1970:

    let days=$(date +%s)/86400
    

提交回复
热议问题