Bash – Check if filename exist from inline command

bash

I have a file named input.txt with this contents:


FILE "Edie – Realities.txt" TXT

And I want to read it, then strip filename path from line that starts with FILE, and check if it exists, so:

[ -f $(cat input.txt | grep FILE | grep -o "\".*\"") ] && echo "exist" || echo "does not exist"

but this outputs:

[: too many arguments  
does not exist

If I run:

echo $(cat input.txt | grep FILE | grep -o "\".*\"")

I get what I expected:

"Edie - Realities.txt"

So why is this, or how can I solve this problem?

Best Answer

$(...) is evidently passing "Edie, -, and Realities.txt" as separate arguments. You need to quote $(...) like any other $variable, and you probably want to remove the "s.

[ -f "$(cat input.txt | grep FILE | sed 's/^.*"\(.*\)".*$/\1/')" ] && echo "exist" || echo "does not exist"
Related Question