Reputation: 43
I would like to subtract sizes of two files. I found location of that files and then I used command:
du -h /bin/ip | cut -d "K" -f1
I got 508 and I wanted to create variable
x=$((du -h /bin/ip | cut -d "K" -f1))
but at the result I got
"-bash: du -h /bin/ip | cut -d 'K' -f1: division by 0 (error token is "bin/ip | cut -d 'K' -f1")"
What did I do wrong? How can i put this value in variable?
Upvotes: 1
Views: 279
Reputation: 140960
What did I do wrong?
You used arithmetic expansion $(( ... ))
instead of a command substitution $( ... )
. As a result shell interpreted /bin
as /
as division and bin
as 0
(because there is no variable named bin
) and tried to divide by 0.
How can i put this value in variable?
Use a command substitution:
x=$(du -h /bin/ip | cut -d "K" -f1)
But it would be way more reliable to use stat for collecting information about files:
x=$(stat -c %s /bin/ip)
To substract two file sizes, you can again use command substitutions to get the size, but use arithmetic expansion to calculate the difference.
difference=$(( $(stat -c %s file1) - $(stat -c %s file2) ))
Upvotes: 2