Bash - 检查文件是否存在,文件名包含空格

cas*_*erl 5 linux bash shell

我正在Bash中测试是否存在文件,其中文件名使用$(printf'%q'"$ FNAME") 进行转义.如下面的注释示例所示,使用if [-f $ FNAME]会产生错误.如何测试包含空格和其他字符的文件名?

#!/usr/bin/env bash

# code used in Raspberry Pi Podcasting Jukebox project
# youtube-dl -f 17 --get-filename https://www.youtube.com/watch?v=AgkM5g_Ob-w
# returns "HOW ABUNDANCE WILL CHANGE THE WORLD - Elon Musk 2017-AgkM5g_Ob-w.3gp"

# Purpose: To test if file exists before downloading
# for testing purposes using an existing regular file "abc def ghi"
AFILE="abc def ghi"
TFILE=$(printf '%q' "$AFILE") # Escaping filename using printf
echo $TFILE # returns abc\ def\ ghi
# if [ -f $AFILE ] # this test returns false every time with error [:too many arguments

if [ -f $TFILE ] # This test also returns FALSE with err [: too many arguments
then
  echo "Existing"
  # don't download
else
  echo "Not existing"
  # youtube-dl http://www.youtube.com/watch?v=AgkM5g_Ob-w
fi
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题的解决方案涉及通过[[]]使测试条件成为Bash表达式的具体问题

Ini*_*ian 8

总是引用你的文件名,%q用于转义空格的想法是对的,但是当与[运算符一起使用时,未加引号$TFILE被分成多个单词,导致-f操作数在实际期望单个参数时接收太多参数.因此,一旦你引用它,就保留了空格,并在条件中传递了一个文字的单个参数.

testFile="abc def ghi"
printf -v quotedFile '%q' "$testFile"

if [ -f "$quotedFile" ]; then
    printf 'My quoted file %s exists\n' "$quotedFile"
fi
Run Code Online (Sandbox Code Playgroud)

以上内容应该适用[于任何POSIX兼容的shell (使用).但是,如果您仅针对bashshell单独定位脚本,则可以使用[[在引用时从不需要引用,因为它作为表达式进行求值.所以你可以做到

file_with_spaces="abc def ghi"
if [[ -f $file_with_spaces ]]; then
    printf 'My quoted file %s exists\n' "$file_with_spaces"
fi
Run Code Online (Sandbox Code Playgroud)

但总的来说,在变量中添加引号并没有什么坏处bash.你总是可以做到.