From: Greg Wooledge <greg@wooledge.org>
To: help-bash@gnu.org
Subject: Re: Adding option to select file types
Date: 05/07/2021 14:50:09 Europe/Paris
On Mon, Jul 05, 2021 at 06:48:14AM +0200, lisa-asket@perso.be wrote:
> I have the following bash function and I want an option that allows me
>
> to pass the file type if I do not want the default .org and .texi
I'm assuming you mean "extensions".
>> print-region ()
>> Â {
>> Â Â Â na=$1
>> Â Â Â nb=$2
>> Â Â Â dir=$3
>> Â Â Â
>> Â Â Â Â Â find "$dir" \( -name \*.org -o -name \*.texi \)Â \
>> Â Â Â Â Â Â Â -exec awk -v a="$na" -v b="$nb"Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
>> Â Â Â Â Â Â Â Â Â Â Â Â Â 'FNR == 1 {s="\n==> "FILENAME" <==\n"; f = 0}
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â FNR == a {print s; f = 1}
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â f {print; if (FNR == b) nextfile}' {} +
>> Â }
>So, here's one possible implementation. I've used bash 3.1 syntax
>features, and placed the documentation in a usage message, in lieu
>of comments.
print-region ()
{
if (($# < 3)); then
echo "usage: print-region startline endline startdir [extension ...]" >&2
echo "If extensions are not provided, .org and .texi will be used." >&2
return 1
fi
local na=$1
local nb=$2
local dir=$3
shift 3
if (($# == 0)); then
set -- .org .texi
fi
# Construct find arguments dynamically from the extension list.
local findargs=( "$dir" '(' )
local ext
for ext in "$@"; do
findargs+=( -name "*$ext" -o )
done
# Overwrite the final -o with ')'
findargs[${#findargs[@]}-1]=')'
find "${findargs[@]}" \
   -exec awk -v a="$na" -v b="$nb" '
     FNR == 1 {s="\n==> "FILENAME" <==\n"; f = 0}
     FNR == a {print s; f = 1}
     f {print; if (FNR == b) nextfile}
' {} +
}
I cannot understand on mhy you use "$dir"
Will have an extension parameter to the function using `-e`, with comma
as delimiter.
For instance
-e texi,org,el
Â