bash関数にファイル形式を渡し、findを呼び出します。

bash関数にファイル形式を渡し、findを呼び出します。

特定のディレクトリにある.texiファイルと.orgファイル形式のファイルを再帰的に監視するために、2行の番号の間を印刷する次の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[@]}"  \

おすすめ記事