I am trying to write a code that runs a script on a list of files determined based on users input. for some reason the following code doesn't work? is there any way to do evalute the query_cmd and iterate over the files it outputs.
if [[ $# -gt 0 && "$1" == "--diff" ]]; then
query_cmd="git diff --name-only '*.cc' '*.h'"
else
query_cmd='find . \( -name "*.cc" -o -name "*.h" \)'
fi
while IFS='' read -r line; do
absolute_filepath=$(realpath "$line")
if [[ $absolute_filepath =~ $ignore_list ]]; then
continue
fi
cpp_filepaths+=("$absolute_filepath")
done < <($query_cmd)
echo "$query_cmd"ifblock inside the< <(...)expression. See "Why does shell ignore quoting characters in arguments passed to it through variables?" and BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!'*.cc'in the$query_cmdstring means the command needs quotes to be honored when it's parsed. When they're not parsed, they're instead treated as literal characters.["git", "diff", "--name-only", "*.cc", "*.h"](JSON-escaped), which is what a shell will do when it parses and executesgit diff --name-only '*.cc' '*.h'as code. Running$query_cmdwill instead run["git", "diff", "--name-only", "'*.cc'", "'*.h'"], adding literal single quotes that in a normally-parsed command would be removed by the shell.