linux - bash 中 if 语句的表达式语法错误

标签 linux bash shell if-statement

代码如下,我想查看一些小于410Bytes的文件:

for file in *; do
    if [[ "$file" =~ ^dataset([0-9]+)$ && `du -b $file/${BASH_REMATCH[1]}_conserv.png` -lt 410 ]]; then
    cd $file
    $some_commands
    cd ..
    fi
done

但是,当我运行这个脚本时,它会这样提示:

less_than_410.bash: line 2: [[: 13605   dataset4866/4866_conserv.png: syntax error in expression (error token is "dataset4866/4866_conserv.png")

有没有人知道如何解决这个问题?谢谢!

最佳答案

du -b file

它将打印文件大小和名称。使用 cut 仅获取大小:

du -b file | cut -f 1

关于linux - bash 中 if 语句的表达式语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13468529/

相关文章:

linux - `mv file.sh/~` 将文件移动到哪里?

regex - 使用 Sed 删除前导空格和尾随空格的问题

bash - 使用系统执行某些操作时如何防止代码注入(inject)?

bash - 简单的 Bash 脚本文件复制

linux - 通过匹配文本替换下一个文件列值中的值列

linux - scp 命令要求输入密码

linux - 在 Linux WSL 中,我的提示符有问题,当我编写多行命令时,命令会在提示符后面开始写入?

python - 在 linux 中获得有关 "Ethernet cable plugged in"事件的通知

regex - 使用 grep 计算包含子字符串的单词数

linux - 如何找到文件的总大小(以字节为单位)?