I made a shell script that finds the size of a directory, returning it in human readable format (e.g., 802M or 702K). I want to calculate the difference between the sizes.
Here’s my shell script so far:
#!/bin/bash
current_remote_dir_size=789M
new_remote_dir_size=802M
new_size=`echo ${new_remote_dir_size} | grep -o [0-9]*`
current_size=`echo ${current_remote_dir_size} | grep -o [0-9]*`
echo "${new_size}-${current_size}"
But the output of the script is just
-
How can I make the subtraction work?
You can do basic integer math in bash by wrapping the expression in
$((and)).In your specific case, the following works for me:
Your output at the end is a bit odd. Check that the grep expression actually produces the desired output. If not, you might need to wrap the regular expression in quotation marks.