简体   繁体   English

如何修复bash脚本中的数组错误?

[英]How to fix a error with array in bash script?

This is a bash script I can run it normally 这是一个bash脚本,我可以正常运行

#!/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 它有错误: expr: syntax error

Change the shebang to #!/bin/bash -x to see how the commands are executed and to see the arguments of the expr when it fails. 将shebang更改为#!/bin/bash -x以查看命令的执行方式,并查看expr失败时的参数。 When you see it, you might know the answer yourself. 看到时,您可能会自己知道答案。 Otherwise, add it to your post. 否则,将其添加到您的帖子中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM