我似乎无法在 bash 脚本中传递带空格的参数

I can't seem to pass arguments with spaces in bash script

我试图编写一个脚本来规范化 Linux 中的权限:

for f in $(find . -type f); do
    file "$f" | grep -e "ELF 64-bit LSB executable" -e "ELF 32-bit MSB executable" > /dev/null
    if [ $? = 0 ]; then
        chmod -c u=rwx,g=rx,o=rx "$f"
    else
        chmod -c u=rw,g=r,o=r "$f"
    fi;
done

显然,我正在尝试将文件路径传递给 chmod,并且我在 "$f" 中使用双引号,但不知何故仍然出现 No such file or directory 错误:

chmod: './FreeDesktop_integration/nautilus-scripts/Archiving/PeaZip/Extract''e erişilemedi: Böyle bir dosya ya da dizin yok
chmod: 'Archive''e erişilemedi: Böyle bir dosya ya da dizin yok

./FreeDesktop_integration/nautilus-scripts/Archiving/PeaZip/Extract Archive 似乎被 chmod 视为 2 个文件(这是相当出乎意料的)。

那么是什么原因造成的,我该如何解决(正确传递参数)?

奖金问题:有没有更好的方法来完成我想用脚本实现的目标?

for f in $(find . -type f)中, shell 对 find 命令的输出执行分词。 像你这样使用它是不安全的。

您可以使用 while 循环来确保安全:

find . -type f -print0 | while IFS= read -r -d '' f; do
    if file "$f" | grep -qe "ELF 64-bit LSB executable" -e "ELF 32-bit MSB executable"; then
        chmod -c u=rwx,g=rx,o=rx "$f"
    else
        chmod -c u=rw,g=r,o=r "$f"
    fi
done

(我还简化了条件,以及来自 @CharlesDuffy 的一堆提示。)