2

In a Debian Linux shell, I am trying to add the results of two separate commands so that I know the sum of both.

I already tried:

echo $(expr $(du -sh /srv/mysql) + $(du -sh /srv/www))  

and variations of it.

It's returning: expr:

"syntax error: unexpected argument ‘/srv/mysql’

"

of course I also tried to paraphrase the folders, etc.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
davidman77
  • 121
  • 3
  • 1
    Not the main issue, but please read: [What is wrong with `echo $(stuff)`?](https://superuser.com/q/1352850/432690) – Kamil Maciorowski Oct 15 '22 at 22:23
  • 1
    Regarding `syntax error: unexpected argument ‘/srv/mysql’` that happens because `du -sh /srv/mysql` returns something like: `500M /srv/mysql` and therefore that's not a correct number. – Edgar Magallon Oct 15 '22 at 23:35
  • 1
    Just use `du -csh /srv/www /srv/mysql` to get the `c`umulative disk usage of both directories (which is not the same as the sum of the disk usage of each run in isolation if both directories contain instances of the same file for instance). – Stéphane Chazelas Oct 30 '22 at 19:00

2 Answers2

3

Like this:

could be used with any shell (bash, zsh, sh, dash...)

echo "$((
    (
        $(du -s /srv/mysql | awk '{print $1}') +
        $(du -s   /srv/www | awk '{print $1}')
    ) / 1024
))MiB"

Or simply:

du -shc /srv/www /srv/mysql | awk 'END{print $1}'

The -c act as the count operator

Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
  • 1
    Good choice about `du -s` instead of `du -sh`. *To avoid cofusion*: About the `MB` at the end of `echo`, it should not be `M` or `MiB`?. Because if I'm not wrong Linux works with `*bibytes`. For example: if `du -sh` shows `323M` it actually means `323MiB` or `323 Mibibytes`. – Edgar Magallon Oct 15 '22 at 22:43
  • 1
    Thanks, edited accordingly – Gilles Quénot Oct 15 '22 at 23:08
1

Assuming you are dealing with integers …

I note that expr is considered archaic and obsolete. It has two main uses, arithmetic and regex matching, both of which are available in most shells.

(By the by, "Debian Linux shell" isn't just one thing; it could be Bash or Dash or indeed whichever shell is chosen when the user is added or changed subsequent.)

For arithmetic, POSIX requires the shell (any shell claiming compliance, including /bin/sh) to evaluate $(( expression )) according to rules similar to those for integer expressions in the C language, so that $((1+2*3)) should give 7.

The main problem with the original question was that other information besides the digits were included in the output. The first thing to try should be to ask the program generating those digits not to output anything else. Only if that fails, then filter the output. There are many many ways to do that, but the simplest is just

tr -dc 0-9

which will remove everything that's not a digit.

If you need decimal fractions …

(decimal fractions are usually fixed point, like 1.23, but floating point systems can understand fixed point numbers as well, so you can use either.)

Neither expr nor $((…)) can cope with decimal fractions. For that you need bc or to use a separate scripting language that has fixed point or floating point built in, such as awk or perl or python.

Martin Kealey
  • 498
  • 3
  • 8