简体   繁体   中英

How to do treat octal numbers as decimal when doing arithmetics under busybox' ash

I have decimal numbers stored in strings.

The numbers which are < 100 are stored in this way "045" or "005" .

When using these number strings in arithmetic operations like let A="045"+"009" these numbers are treated as octal numbers like indicated in the man page.

To treat them as decimal I added 10# at the beginning of the number strings like that

let A="10#045"+"10#123"

but this solution causes an error -ash: let: arithmetic syntax error in my bash from BusyBox (Installed on OpenWRT)

Is there another solution for my busybox shell?

Note : The operation should evaluated with let because I need theses numbers in other kind of operations like bitwise operation.

busybox does not have bash , its shell is ash .

You can either strip the leading zeros off your variables, eg:

while [ "${n:0:1}" = "0" ]; do n="${n#?}"; done

or use expr :

$ echo $(expr 045 + 045)
90

You can remove the zeros before doing the arithmetics:

n=005
shopt -s extglob
n1=${n##+(0)}
echo $n1

Output:

5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM