1

我无法以某种方式完成这项工作,

(( 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 ",错误本身的格式不正确。

4

3 回答 3

2

您的$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 }'
于 2012-05-03T11:16:55.503 回答
0

确保 $size 和 $chunk_size 已初始化...已测试:

size=1
chunk_size=2
(( chunks = $size / $chunk_size + 1 ))
echo $chunks
于 2012-05-03T09:39:53.283 回答
0

尝试这个:

let size=10
let chunk_size =2
declare -i chunks
let chunks =  $(($size / $($chunk_size + 1))))
echo $chunks
于 2012-05-03T11:04:27.330 回答