grep は `grep:: 対応するファイルやディレクトリがありません`と文句を言います。

grep は `grep:: 対応するファイルやディレクトリがありません`と文句を言います。

スクリプトに対して次のテストが作成されました。

grep苦情を受け取るには、次の方法で電話をかけます。

test --FS="," --incl=.texi,.org -C 8 --dyn="package" ./01cuneus
grep: : No such file or directory

fdir問題の原因である可能性がある空の要素が含まれていることがわかりました。

test ()
{

  local fs=""  fdir=()  incl=()
  local dyn=0  ctx=0  fdir=""
  local isufx=()  ictx=()
 
  shrtopts="C:"
  longopts="FS:,incl:,dyn:,dynamic:,context:"
  
  opts=$(getopt -o "$shrtopts" -l "$longopts" -n "${0##*/}" -- "$@")

  eval "set -- ${opts}"
  while (( $# > 0 )); do
    case "$1" in
      # ----------------------------------------------
      ("--FS")
        fs="$2" ; shift 2 ;;
      ("--incl")
        incl+=("$2") ; shift 2 ;;
      # ----------------------------------------------
      ("--dyn"|"--dynamic")
        dyn=1 ; ptrn="$2" ; shift 2 ;;
      ("-C"|"--context")
        ctx=$2 ; shift 2 ;;
      # ----------------------------------------------
      ("--")
        shift ; break ;;
      # ----------------------------------------------
    esac
  done

  if (( $# >= 1 )); then
    
    declare -A tag                # declare Associative Array
    for dpa in "$@"; do
      [[ ! -d $dpa ]] && continue  # skip to next iteration
      [[ ${tag[din:$dpa]} ]] && continue
      fdir+=("$dpa")
      tag[din:$dpa]=1
    done

  fi

  [[ ! -z "$ctx" ]] && ctx=8
  ictx=( -C "$ctx" )

  (( ${#incl[@]} == 0 )) && incl=( .texi .org )
  
  if (( ${#incl[@]} > 0 )); then

    for ext in "${incl[@]}"; do
      s="$ext"
      [[ (! -z "$fs") && ("$ext" == *"$fs"*) ]] && s=${ext//"$fs"/" "}
      for fltyp in $s; do
        isufx+=( --include="*$fltyp" )
      done
    done
    
  fi
    
  grep -rl "${isufx[@]}" "$ptrn" "${fdir[@]}" |
    while read f; do
      echo -e $(tput setaf 46)"==> $f <==\n"$(tput sgr0)
      grep -ni "${ictx[@]}" "$ptrn" "$f"
      echo ""
    done

}

ベストアンサー1

あなたの機能にはtest最初に2行があります

  local fs=""  fdir=()  incl=()
  local dyn=0  ctx=0  fdir=""

空の文字列が変数fdirにどのように割り当てられるかを確認してください。

後であなたは追加データをfdir配列として使用します。これにより、配列の先頭に空の要素が残り、fdir説明する問題が発生します。

fdir空の文字列で初期化しないでください。

おすすめ記事