Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 732  / 3 Years ago, sun, october 24, 2021, 10:59:20
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 ?


More From » bash

 Answers
2

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

[#9234] Monday, October 25, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
throecember

Total Points: 274
Total Questions: 118
Total Answers: 132

Location: India
Member since Thu, Jun 11, 2020
4 Years ago
;