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

时间:2017-11-06 21:46:28

标签: git bash

我有一个来自文件源的函数,它检查感兴趣的文件是否有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传递文件列表

1 个答案:

答案 0 :(得分:1)

你没有。您将$patn设为数组,然后将"${patn[@]}"放在要使用它的位置。

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