我怎么也做不到,

(( chunks = $size / $chunk_size + 1 ))
echo $chunks

编辑
我也试过,
chunks = $(( $size / $chunk_size + 1 ))

虽然这有效,
while [ $counter -le 10 ]; do
    echo $counter;
    (( counter++ ))
done

前者给我/ 157286400 + 1 ")syntax error: invalid arithmetic operator (error token is "并且错误本身格式不好。

最佳答案

您的$size变量未初始化或包含其他非数字字符。

$ chunk_size=157286400
$ unset size
$ (( chunks = $size / $chunk_size + 1 ))
-bash: ((: chunks =  / 157286400 + 1 : syntax error: operand expected (error token is "/ 157286400 + 1 ")
$ size=$'\r'  # carriage return
$ (( chunks = $size / $chunk_size + 1 ))
 / 157286400 + 1 ")syntax error: operand expected (error token is "

您可以通过如下修改awk命令来删除回车:
awk '/^Content-Length:/ { gsub("\015", "", $2); print $2 }'

关于linux - 如何在Bash/Ubuntu 12.04中正确编写此算术表达式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10428691/

10-10 11:16