linux - 语法错误: operand expected (error token is “+” ) (i coudn't resolve this problem with the other thread)

标签 linux shell

我无法让这个简单的程序运行,代码看起来像这样,因为这是为了类练习,我不会那样做,但我必须这样做,抱歉,如果代码一团糟,但我尝试了很多东西,代码有点“变形”

n= 0
for x in /home
do
e= du $x -B1 | cut d" " -f 1
$sum$(($sum+$e))
done
echo $sum

最佳答案

在第 1 行,“=”和 0 之间有一个空格,但赋值中“=”前后不应有空格。

在第 4 行发生了同样的情况,但是您还错过了命令周围的反引号“`”,这表明 bash 评估反引号内的内容并返回该命令的输出。

第 5 行显示:

$sum$(($sum+$e))

那么你的意思是:

sum=$(($sum+$e))
<小时/>

更新:我又发现了三个问题:

在第 2 行中,将 /home 替换为 /home/*,因为前者在循环中仅使用/home,后者返回/home 目录中的每个目录(和文件)。

您正在将 d"" 传递给 cut,正确的选项是 -d ""

此外,du 输出的格式是制表符,而不是空格。如果删除cut中的-d "",它就可以工作。

关于linux - 语法错误: operand expected (error token is “+” ) (i coudn't resolve this problem with the other thread),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54390838/

相关文章:

c++ - 如何用C++实现Linux的Script命令

python - 为什么 virtualenv 不虚拟化任意目录?

php - 在文件夹中的所有 PHP 文件中用新词替换所有包含特定词的行

shell - 使用 shell 脚本操作文件名 : changing file extensions

shell - 为 Vim 命令的 -complete 属性设置多个值

linux - 重新绑定(bind)正常模式控件 vi zsh

无法通过 system() 从程序中设置 "echo"文件

linux - 如果 [ "$state"== 1 ] ... 永远不等于 1

shell - Elastic Beanstalk cron 和部署权限

linux - 如何在shell中处理10多个参数