ART
ART

Reputation: 1569

How to store big values in shell script variable

When I store big values in variable busybox shell script and print, it prints -ve values.
I know shell doesn't have variable type but then how do I avoid this folding back to -ve values ?
I used below command commands in shell and it prints -ve value.

# let b=3841982464
# echo $b
-452984832

Any suggestion/pointer ?

EDIT:
Answer suggested by agilob works for my original question but my actual problem is, I was last to go to end of disk - 17kb So I used command as follows

# b=$(($(blockdev --getsize64 /dev/mmcblk0) - $((17*1024))))
# echo $b
-453002240

So still value is printed -ve :(, using expr also gives weird result as follows

# b=`expr $(blockdev --getsize64 /dev/mmcblk0) - 17408`
# echo $b
2147466239

Answer to actual problem
so original question was answered by agilob so I accepted that answer. And actual problem I could solve as follows,

# b=$(blockdev --getsize64 /dev/mmcblk0)
# echo $b
3841982464
# echo "$b 17408 - p" | dc
3841965056

AWK way

I tried doing it in awk way as user1934428 suggested, it can be done as follows,

# echo "$(blockdev --getsize64 /dev/mmcblk0) 17408" | awk '{ printf "%.0f", $1 - $2}'
3841965056#

Upvotes: 1

Views: 2876

Answers (2)

user1934428
user1934428

Reputation: 22301

You basically want to do arithmetic with "arbitrary long integers". This is not built into bash (which uses 8 byte integer).

I would store the numbers in just normal environment variables and use other programming languages (for instance Ruby) to process them.

Upvotes: 1

agilob
agilob

Reputation: 6233

Why not just:

b=3841982464
echo $b
   => 3841982464

Upvotes: 3

Related Questions