Issue
I'm trying to find all the SQL files that have BOM, using the following bash script:
#!/bin/bash
set -x
printf '\nStart!\n'
caminho="$PWD"
parametros="-type f -name "*.sql""
$(find ${caminho} ${parametros} -exec grep -l $'\xEF\xBB\xBF' {} \;)
But I'm always getting this output:
find /home/pablo/SQL -name ComBOM.sql ComVirgula.sql SemBOM.sql -exec grep -l $\xEF\xBB\xBF {} ;
find: paths must precede expression: `ComVirgula.sql'
find: possible unquoted pattern after predicate `-name'?
There's a lot of questions regarding this problem, which were solved by adding double quotes on the -name
, but this didn't work for me. And this is also the same thing that the man says.
Solution
Adding pairs of unescaped (thus, syntactic) quotes inside a string quoted with the same type of quote characters does nothing at all, because those quotes just cancel each other out. Whereas if you did escape the quotes, they would be treated as data instead of syntax at expansion time, so the code still wouldn't behave as-desired but with a different error (a situation BashFAQ #50 goes into detail on).
Use an array, not a string, so you can pass your arguments through exactly as-quoted without unwanted globbing.
Instead of:
parametros="-type f -name "*.sql""
...use instead:
parametros=( -type f -name "*.sql" )
and then later:
find "$PWD" "${parametros[@]}" -exec grep -l $'\xEF\xBB\xBF' {} +
Answered By - Charles Duffy