Bash 脚本语法错误:预期操作数(错误标记为“=”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43373138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:01:18  来源:igfitidea点击:

Bash script syntax error: operand expected (error token is "=")

bashsyntax

提问by Jürgen K.

What I need to do is to cut a long text file into 10 peaces with same count of lines. Therefore I wrote the following script.

我需要做的是将一个长文本文件以相同的行数切割成 10 个和平区。因此我编写了以下脚本。

#!/usr/bin/bash
filename=""
count=0
file=0
br=$(wc -l $filename | awk '{print }')
let br = $br/10
while read -r line
do
let count = count + 1
    name="$line"
echo $name >> file$file.csv
if [ $count = $br ];then
let count=0
let file+=1
fi
done < "$filename"

The scripts produces the following error which I do not understand

脚本产生以下错误,我不明白

cut.sh: line 9: let: =: syntax error: operand expected (error token is "=")

I have read similar topicbut still could not find a solution. Any ideas are welcome. Thanks

我已经阅读了类似的主题,但仍然找不到解决方案。欢迎任何想法。谢谢

回答by heemayl

bashvariable assignment can not have whitespace around =. Drop the spaces around =:

bash变量赋值不能有空格=。删除周围的空格=

let br=${br}/10

Do the same for all such cases.

对所有此类情况执行相同操作。

回答by Raman Sailopal

Mathematically calculations in bash need to be enclosed in double brackets and so:

bash 中的数学计算需要用双括号括起来,因此:

let count=$(( $count + 1 ))