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

前端 未结 8 448
情深已故
情深已故 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:27

    Here's an easy way, albeit not the prettiest way to get an int value for a string.

    hour=`expr $hour + 0`
    

    Example

    bash-3.2$ hour="08"
    bash-3.2$ hour=`expr $hour + 0`
    bash-3.2$ echo $hour
    8
    

提交回复
热议问题