AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1108810
Accepted
singrium
singrium
Asked: 2019-01-12 02:09:22 +0800 CST2019-01-12 02:09:22 +0800 CST 2019-01-12 02:09:22 +0800 CST

删除文件的 shell 脚本不起作用

  • 772

我想删除多张分辨率小于 228x228 的图像。为此,我编写了这个 shell 脚本:

#!/bin/bash

for i in $( ls ); do
    if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
    then
        rm $i
    fi
done

由于某些原因,我在运行它时得到了这个输出:

./del.sh: line 4: [640: command not found
./del.sh: line 4: [550: command not found
./del.sh: line 4: [315: command not found
...

你能告诉我这个脚本有什么问题以及如何解决它。
谢谢你。

编辑:即使我在括号后添加了空格,我仍然得到一个错误。这是由于使用了<而不是,-lt并且已得到修复。现在没有错误。

scripts command-line bash
  • 3 3 个回答
  • 1187 Views

3 个回答

  • Voted
  1. Best Answer
    PerlDuck
    2019-01-12T02:28:17+08:002019-01-12T02:28:17+08:00

    这里有一些问题:首先,[…]测试中的表达式需要围绕它的空格(陷阱#10),其次,比较<不适用于[…]测试(陷阱#7)。您要么需要-lt(小于),要么使用[[…]]代替,这是一种bashism。此外,for应该更换循环(陷阱#1)。

    所以:

    for i in ./*; do
        if [ -e "$i" ]; then
            if [ $(identify -format "%w" "$i") -lt 228 ] && [ $(identify -format "%h" "$i") -lt 228 ];
            then
                rm -- "$i"
            fi
        fi
    done
    

    您可能还希望避免调用identify两次来获取两个维度(陷阱 #58),而是只调用一次,并让它打印一个准备用作 shell 语法中的变量赋值的字符串。

    如果我们写

    identify -format "width=%w height=%h" "$i"
    

    它会打印出类似width=50 heigth=250. 当我们eval使用该字符串时,我们只需一次调用就设置了两个变量,条件可以写成:

    eval "$(identify -format "width=%w height=%h" "$i")"
    if [ $width -lt 228 ] && [ $height -lt 228 ];
    then
        rm -- "$i"
    fi
    

    另请参阅:常见的 bash 陷阱。

    • 11
  2. pLumo
    2019-01-12T04:10:32+08:002019-01-12T04:10:32+08:00

    而不是循环,我会使用findwith -execand -delete:

    find . -maxdepth 1 -type f  \
       -exec sh -c '
           [ $(identify -format "%w" "$1") -lt 228 ] &&
           [ $(identify -format "%h" "$1") -lt 228 ]' _ {} \; \
       -delete -print
    

    这还将打印被删除的文件,-print如果您不希望这样做,您可以删除。

    • 6
  3. theist
    2019-01-12T04:56:38+08:002019-01-12T04:56:38+08:00

    不打算回答,而是提供一个有用的提示,帮助我编写 bash 脚本。

    有一个名为 linter 的 shell script lintershellcheck可能会捕获 bash 脚本中的一些常见错误并避免一些陷阱。它可以像 ubuntu 中的任何软件包一样安装 -> https://launchpad.net/ubuntu/+source/shellcheck用于universe当前稳定版。

    这是您的脚本的输出

    shellcheck del.sh
    
    In del.sh line 4:
        if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
        ^-- SC1009: The mentioned parser error was in this if expression.
           ^-- SC1073: Couldn't parse this test expression.
            ^-- SC1035: You need a space after the [ and before the ].
                                              ^-- SC1020: You need a space before the ].
                                              ^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
    

    如果您修复并再次申请,您将获得已在接受的答案中提到的其他一些建议和修复。

    • 3

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve