我已经编写了一个脚本来列出我的存储库中的所有本地分支。
#!/bin/bash
clear
branches=()
menuposition=0
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
for branch in "${branches[@]}"; do
menuposition=$((menuposition+1))
echo "$menuposition) $branch"
done
输出是......
1) master
2) foo
3) bar
我可以使用read命令获取用户输入。但是,......我怎样才能检查用户选择的分支?
答案 0 :(得分:4)
#!/bin/bash
clear
options=()
#for bash version 4 or higher use mapfile.
#Fallback to while loop if mapfile not found
mapfile -t options < <(git for-each-ref \
--format='%(refname:short)' refs/heads/) &>/dev/null \
|| while read line; do options+=( "$line" ); done \
< <(git for-each-ref --format='%(refname:short)' refs/heads/)
options+=('Exit')
select opt in "${options[@]}"
do
if [[ "$opt" ]] && [[ "$opt" == 'Exit' ]]; then
echo "Bye Bye"
exit 0
elif [[ "$opt" ]]; then
git checkout "$opt"
else
echo "Wrong Input. Please enter the correct input"
fi
done