我在使用腳本從已經合并到 master 的 repo 中洗掉舊分支時遇到了一些問題。
我有兩個分支我想排除并嘗試過這個操作:
for branchname in `git branch -r --merged origin/master`;
if [[ "$branchname" != "origin/master" || "$branchname" != "origin/automation" ]]; then
*** delete branches ***
當我運行它時,"origin/master" 使它成為 if 陳述句。
但如果我只運行:
if [[ "$branchname" != "origin/master" ]]; then
***delete branches***
那么“origin/master”不會進入if陳述句,這是正確的。
我在 if/or 中做錯了什么?
順便說一句,我在 Jenkins shell 腳本中運行它
uj5u.com熱心網友回復:
Bash 是正確的,您的腳本邏輯有缺陷。
如果$branchname是,origin/master那么它仍然匹配第二個條件 ( $branchname != origin/automation) 并且整個條件是true因為 OR 運算子 ( ||)。
您可能想&&在您的條件下使用 AND ( ) :
for branchname in `git branch -r --merged origin/master`;
if [[ "$branchname" != "origin/master" && "$branchname" != "origin/automation" ]]; then
# the branch is neither `origin/master` nor `origin/automation`
# do something with it
fi
done
uj5u.com熱心網友回復:
迭代讀取而不是拆分回傳的字串:
git branch -r --format '%(refname)' --merged origin/master |
while read -r refname; do
branchname="${refname#refs/remotes/}"
case "$branchname" in
origin/master ) continue ;;
origin/automation ) continue ;;
*)
printf 'Branch %s is neither origin/master or origin/automation\n' \
"$branchname"
;;
esac
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315925.html
標籤:猛击
