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

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

    what I'd call a hack, but given that you're only processing hour values, you can do

     hour=08
     echo $(( ${hour#0} +1 ))
     9
     hour=10 
     echo $(( ${hour#0} +1))
     11
    

    with little risk.

    IHTH.

    0 讨论(0)
  • 2020-12-12 21:39

    You could also use bc

    hour=8
    result=$(echo "$hour + 1" | bc)
    echo $result
    9
    
    0 讨论(0)
提交回复
热议问题