I am testing in Bash for if a file is existing, where the file name is escaped using $(printf '%q' "$FNAME")
.
This always produces an error using if [ -f $FNAME ]
as in the commented example below. How can I test for a filename that contains spaces and other characters?
#!/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