Subtract two variables in Bash

后端 未结 8 1925
时光取名叫无心
时光取名叫无心 2020-12-02 06:33

I have the script below to subtract the counts of files between two directories but the COUNT= expression does not work. What is the correct syntax?

<         


        
8条回答
  •  春和景丽
    2020-12-02 07:19

    You just need a little extra whitespace around the minus sign, and backticks:

    COUNT=`expr $FIRSTV - $SECONDV`
    

    Be aware of the exit status:

    The exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null or 0.

    Keep this in mind when using the expression in a bash script in combination with set -e which will exit immediately if a command exits with a non-zero status.

提交回复
热议问题