![bash 함수에 파일 형식을 전달하고 find를 호출합니다.](https://linux55.com/image/192640/bash%20%ED%95%A8%EC%88%98%EC%97%90%20%ED%8C%8C%EC%9D%BC%20%ED%98%95%EC%8B%9D%EC%9D%84%20%EC%A0%84%EB%8B%AC%ED%95%98%EA%B3%A0%20find%EB%A5%BC%20%ED%98%B8%EC%B6%9C%ED%95%A9%EB%8B%88%EB%8B%A4..png)
특정 디렉터리에 있는 .texi 및 .org 파일 형식의 파일을 재귀적으로 모니터링하기 위해 두 줄 번호 사이를 인쇄하는 다음 bash 함수가 있습니다.
구분 기호로 사용되는 옵션을 사용하여 -e
검색할 파일 확장자를 제공하고 싶습니다 ,
. 예: -e el,texi,org
print-region ()
{
# Process command line options
shortopts="hvd:e:p:q:"
longopts="help,version,directory:,extension:,startline:,stoplinene:,"
opts=$(getopt -o "$shortopts" -l "$longopts" -n "$(basename $0)" -- "$@")
if [ $? -eq 0 ]; then
eval "set -- ${opts}"
while [ $# -gt 0 ]; do
case "$1" in
--)
shift; break ;;
-h|-\?|--help)
help=1
local -r f=0
break
;;
-v|--version)
version=1; shift; break ;;
-d|--directory)
local -r dir=$2
shift 2
;;
-e|--extension)
fltype=$2
shift 2
;;
-p|--startline)
local -r na=$2; shift 2 ;;
-q|--stopline)
local -r nb=$2; shift 2 ;;
esac
done
else
shorthelp=1 # getopt returned (and reported) an error.
fi
if [[ $na =~ ^[[:digit:]]+$ ]] && [[ $nb =~ ^[[:digit:]]+$ ]] \
&& [[ -d $dir ]]; then
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}' {} +
fi
}
답변1
이미 처음에 솔루션을 시도했으며 의견을 보내 주시면 감사하겠습니다. 축하합니다.
str=$(echo $fltype | tr "," "\n")
nmser=( '(' )
for ext in $str; do
nmser+=( -name "*.$ext" -o )
done
nmser[${#nmser[@]}-1]=')'
printf "nmser:\n"
for i in ${nmser[@]}; do echo $i; done
if [[ $na =~ ^[[:digit:]]+$ ]] && [[ $nb =~ ^[[:digit:]]+$ ]] \
&& [[ -d $dir ]]; then
# `-exec` avoids problems with too many file matches
# `nextfile` stops processing the current input file.
#find "$dir" \( -name \*.org -o -name \*.texi \) \
find "$dir" "${nmser[@]}" \