This is a bash script I can run it normally
#!/bin/bash
array=$(awk '{print $4}' /var/log/httpd/access_log | uniq -c | cut -d[ -f1)
sum=0
sum1=0
arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for (( i=1; i<${#arr[@]}; i++ ));
do
sum=$( expr $sum - ${arr[$i]} )
sum1=$( expr $sum1 + $sum )
done
echo
echo "Sum of \$arr = ${sum1}"
exit $sum
but When I change
sum=$( expr $sum - ${arr[$i]} )
by
sum=$( expr ${arr[$i+1]} - ${arr[$i]} )
or
j=$( expr $i + 1) sum=$( expr ${arr[$j]} - ${arr[$i]} )
it has error: expr: syntax error
#!/bin/bash -xto see how the commands are executed and to see the arguments of theexprwhen it fails. When you see it, you might know the answer yourself. Otherwise, add it to your post.arrayis not an array assignment!expryou can dosum=$(( ${arr[$i+1]} - ${arr[$i]} ))for arithmetic expansion. You could possibly also uselet...