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

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

    In Short: In order to deal with "Leading Zero" numbers (any 0 digit that comes before the first non-zero) in bash - Use bc An arbitrary precision calculator language

    Example:

    a="000001"
    b=$(echo $a | bc)
    echo $b
    

    Output: 1

    From Bash manual:

    "bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming lan- guage. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.)"

提交回复
热议问题