bash将字符串中的单词分隔为子shell中的单独输入



我有一个来自我源的文件的函数,用于检查感兴趣的文件是否有 git diff。如何为$patn分配值,以使diffs=$(git diff --name-only "$ancestor".."$source_ref" -- "$patn")按预期工作?

build_diff () {
  local patn
  patn=${1:-"."}
  # convert names to hash
  local this_ref
  this_ref=${2:-$(git rev-parse --verify HEAD)}
  local source_ref
  source_ref=$(git show-ref -s --heads "$this_ref" || echo "$this_ref")
  local target_ref
  target_ref=$(git show-ref -s --heads "${3:-master}" || echo "$3")
  # when target and source are the same (post integration), use the branch's
  # parent as ancestor
  # When they are not the same, find the 'best' common node as ancestor
  local ancestor
  if [[ $target_ref == "$source_ref" ]]; then
    ancestor=$(git rev-parse --verify "$target_ref"^)
  else
    ancestor=$(git merge-base --all "$target_ref" "$source_ref" || git merge-base --fork-point "$target_ref" "$source_ref")
  fi
  local diffs
  diffs=$(git diff --name-only "$ancestor".."$source_ref" -- "${patn[@]}")
  echo $diffs
  if [[ -z ${diffs//[[:space:]]/} ]]; then
    echo false
  else
    echo true
  fi
}

-- 函数调用示例

FILES=".tool-versions ./other/runner/Dockerfile"
build_diff "$FILES"

如果在脚本中我忘记了 1 美元并且patn=( .tool-versions ./other/runner/Dockerfile )那么它可以工作,但我无法通过 $1 传入文件列表

你没有。你$patn一个数组,然后把"${patn[@]}"放在你想使用它的地方。

$patn=(foo bar baz)
 ...
echo "${patn[@]}"

相关内容

最新更新