Convert string into integer in bash script - “Leading Zero” number error

前端 未结 8 447
情深已故
情深已故 2020-12-12 20:43

In a text file, test.txt, I have the next information:

sl-gs5 desconnected Wed Oct 10 08:00:01 EDT 2012 1001

I want to extract the hour of

8条回答
  •  情歌与酒
    2020-12-12 21:16

    See ARITHMETIC EVALUATION in man bash:

    Constants with a leading 0 are interpreted as octal numbers.

    You can remove the leading zero by parameter expansion:

    hour=${hour#0}
    

    or force base-10 interpretation:

    $((10#$hour + 1))
    

提交回复
热议问题