help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Adding option to select file types


From: lisa-asket
Subject: Adding option to select file types
Date: Wed, 7 Jul 2021 06:33:55 +0200 (CEST)

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





  






reply via email to

[Prev in Thread] Current Thread [Next in Thread]