0
find_totalusage()
{
    local totalusedsize=0
    for dirname in $dirnamelist
    do
        dirsize=$(find_dir_size "$dirname")
        totalusedsize=$(( $totalusedsize + $dirsize ))
    done
    echo $totalusedsize
}

size=$(find_totalusage)

Any one help me find the error ?

2 Answers2

1

I think the most likely explanation is that your dirsize variable is empty.

For example, in bash

$ foo=3; bar= ; echo $(( $foo + $bar ))
bash: 3 +  : syntax error: operand expected (error token is "+  ")

while in AT&T ksh

$ foo=3; bar= ; echo $(( $foo + $bar ))
ksh:  3 +  : more tokens expected

and in BSD mksh

$ foo=3; bar= ; echo $(( $foo + $bar ))
mksh:  3 +  : unexpected 'end of expression'

Note that all of these shells allow "bare" variable names like (( foo + bar )) in expressions, and this form may be preferable since it treats empty variables as zero values. From man bash:

   Shell variables are allowed as operands; parameter  expansion  is  per‐
   formed before the expression is evaluated.  Within an expression, shell
   variables may also be referenced by name without  using  the  parameter
   expansion  syntax.  A shell variable that is null or unset evaluates to
   0 when referenced by name without using the parameter expansion syntax.

So for example

$ foo=3; bar=7 ; echo $(( foo + bar ))                                         
10

$ foo=3; bar= ; echo $(( foo + bar ))                                          
3
steeldriver
  • 142,475
-1

man bash documents the for command as:

for name [ [ in [ word ... ] ] ; ] do list ; done

man ksh says:

for name [in word ...]; do list; done

In both cases, you need a semicolon (;) to mark the end of the expression containing the words in $dirlist.

waltinator
  • 37,856