[英]How could I parse an output command with shell scripting?
我正在开发一个shell脚本,该脚本是使用Whiptail的Git接口(shell接口)。
我的问题是,我想创建一个菜单,我们可以在其中选择Git存储库的分支并将此字符串返回到脚本。
我知道如何通过以下方式获得Git分支的不同名称:
git branch
但我不知道如何解析它们以将其传达给Whiptail。
我想统计分支的数量,并以选项的形式逐个进行交流。
例如:
$> git branch
master
asm
release
debug
我想在shell脚本中将Whiptail用作:
Here the number of branch
v
whiptail --tittle "Branch select" --menu "Choose your branch" 20 60 4 \
"1" "master" \
"2" "asm" \
"3" "release" \
"4" "debug"
但是我不知道如何解析git branch
的输出以将其作为shell脚本获取,请问能帮我吗?
编辑:我终于做到了:) 源代码
我不知道什么是whiptail
,但是前一阵子我写了一个bash函数来列出您最近访问过的所有分支,并为用户提供一个菜单来选择一个。
来源在我在Github上的.bashrc
中 ,我已经在这里复制了它:
# Colors for prompt
COLOR_RED=$(tput sgr0 && tput setaf 1)
COLOR_GREEN=$(tput sgr0 && tput setaf 2)
COLOR_YELLOW=$(tput sgr0 && tput setaf 3)
COLOR_DARK_BLUE=$(tput sgr0 && tput setaf 4)
COLOR_BLUE=$(tput sgr0 && tput setaf 6)
COLOR_PURPLE=$(tput sgr0 && tput setaf 5)
COLOR_PINK=$(tput sgr0 && tput bold && tput setaf 5)
COLOR_LIGHT_GREEN=$(tput sgr0 && tput bold && tput setaf 2)
COLOR_LIGHT_RED=$(tput sgr0 && tput bold && tput setaf 1)
COLOR_LIGHT_CYAN=$(tput sgr0 && tput bold && tput setaf 6)
COLOR_RESET=$(tput sgr0)
# git
function _c() {
cur=${COMP_WORDS[COMP_CWORD]}
branches=`git for-each-ref --sort=-committerdate refs/heads/ | head -n 10`
output=''
for branch in $branches
do
output+=`echo "$branch" | sed 's/.*refs\/heads\///'`
# creative way to get color here? echo messes everything up
# (http://unix.stackexchange.com/questions/107417/what-are-the-special-characters-to-print-from-a-script-to-move-the-cursor ?)
#output+=" '`git show --quiet $(echo $branch | cut -d' ' -f1) --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset'"`"$'\n'
#echo " \'`git show --quiet $(echo $branch | cut -d' ' -f1) --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset\'"`"$'\n'
output+=" \'`git show --quiet $(echo $branch | cut -d' ' -f1) --pretty=format:"%h <%an> %s (%cr)\'"`"$'\n'
done
response=''
for branch in $output
do
lowerBranch=`echo $branch | tr '[:upper:]' '[:lower:]'`
if [[ $branch =~ .*$cur.* ]]; then
response+=$branch$'\n'
fi
done
COMPREPLY=( $( compgen -W "$response" -- $cur ) )
}
function c() {
newBranch=""
inputted=""
if [[ -z "$1" ]]; then
branchOutput=`git for-each-ref --sort=-committerdate refs/heads/ | head -n 10`
declare -a branches
let xx=0
IFS=$'\n'
pad=$(printf '%0.1s' " "{1..32})
padlength=32
for branch in $branchOutput
do
# Show them in a list with a counter
xx=`expr $xx + 1`
branches=("${branches[@]}" "$branch")
branchName=`echo "$branch" | sed 's/.*refs\/heads\///'`
string1="$COLOR_PURPLE$xx. $COLOR_PINK $branchName"
uncolor="$xx. $branchName"
printf '%s' $string1
printf '%*.*s' 0 $((padlength - ${#uncolor} )) "$pad"
printf '%s\n' `git show --quiet $branchName --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset"`
done
# Prompt user for file. -n means no line break after echo
echo -n "$COLOR_YELLOW?$COLOR_RESET "
read branchNumber
let "branchNumber+=-1"
if [[ "$branchNumber" =~ ^[0-9]+$ ]]; then
newBranch=`echo "${branches[@]:$branchNumber:1}" | sed 's/.*refs\/heads\///'`
if [[ -z "$newBranch" ]]; then
echo "Not real."
return 1
fi
else
echo "Wtf?"
return 1
fi
else
inputted=1
newBranch=`echo "$1" | cut -d' ' -f1`
fi
if [[ -n "$1" ]]; then
echo `git show --quiet "$newBranch" --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset"`
fi
if [[ $newBranch =~ ^pr ]]; then
echo -e "\ngit fetch $newBranch && git checkout $newBranch"
git fetch $newBranch && git checkout $newBranch
else
echo -e "\ngit checkout $newBranch"
git checkout $newBranch
fi
}
# add autocompletion to the c command
complete -F _c c
如果将其放在~/.bashrc
并在命令行中运行source ~/.bashrc
(或打开新的shell),则只需在git repo中运行命令c
,您将获得如下输出:
或者,您可以简单地键入c
tab tab来自动完成分支名称:
这花了很多天才能完成,我不希望任何人都经历这个过程。 编写bash来制作合理的UI很麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.