Here is a simplified code that prints the name of Directory if it contains a Filename with same name as the parent directory and .md
extension.
FIND(){
find . -type d -exec sh -c '
for d do
[ -f "${d}/${d##*/}.md" ] && printf "%s\n" "$d"
done' find-sh {} +
}
FIND
To generalize I want to send the Search term ${d}/${d##*/}.md
as an argument to the FIND
function, but unfortunately this does not outputs anything:
FIND(){
local SearchTerm="${1}"
find . -type d -exec sh -c '
for d do
[ -f "${SearchTerm}" ] && printf "%s\n" "$d"
done' find-sh {} +
}
FIND '${d}/${d##*/}.md'
I am sure there is some issue with the quotation of the SearchTerm
. Any hints?
I tried: FIND '\${d}/\${d##*/}.md'
but has no output
SearchTerm
variable, if you want to pass it to child process. Instead oflocal SearchTerm=...;
, writeSearchTerm=$1 find ...
.$1
in things likefunc(){ VAR=$1 find ... ; }
${d}/$...
still won't work, because the shell does not expand variables recursively.SearchTerm
while invocation of the function. Also, may I ask you why we don't need to quote$1
as you wrote in the previous comment.f(){ v=$1 printenv v; }; f '* *'
. I think that @Philippes' solution, which could be simplified to justFIND(){ find ... sh -c '... [ -f "'"$1"'" ] && ...' ...; }
is fine for what you want to achieve, and trying to "improve" it (with eg. backquoting"
s in the interpolated variable) will only break it.