Bash regex没有';不匹配命令行
如何使Bash regex没有';不匹配命令行,regex,bash,shell,unix,posix,Regex,Bash,Shell,Unix,Posix,如何使git-commit调用我编写的自定义包装shell脚本(例如~/.gitcommit),但所有其他git命令都像普通命令一样传递给git 我正在努力,正如 preexec(){ 回声预执行“$1” 如果[[“$1”=~^[:space:]*git[:space:]+commit[:space:]]],则 检测到echo git提交 fi } preexec\u invoke\u exec(){ [-n“$COMP_LINE”]&返回#如果完成,则不执行任何操作 [“$BASH_COMMA
git-commit
调用我编写的自定义包装shell脚本(例如~/.gitcommit
),但所有其他git
命令都像普通命令一样传递给git
我正在努力,正如
preexec(){
回声预执行“$1”
如果[[“$1”=~^[:space:]*git[:space:]+commit[:space:]]],则
检测到echo git提交
fi
}
preexec\u invoke\u exec(){
[-n“$COMP_LINE”]&返回#如果完成,则不执行任何操作
[“$BASH_COMMAND”=“$PROMPT_COMMAND”]&&return#不要导致$PROMPT_命令的preexec
本地this_命令=`HISTTIMEFORMAT=history 1 | sed-e“s/^[]*[0-9]*[]*/';
preexec“$this_命令”
}
陷阱“preexec\u invoke\u exec”调试
但是这个印的像这样
$ git commit
preexecing git commit
fatal: not a git repository (or any of the parent directories): .git
$ git commit with some args
preexecing git commit with some args
fatal: not a git repository (or any of the parent directories): .git
$ git commit now now now
preexecing git commit now now now
fatal: not a git repository (or any of the parent directories): .git
$ git commit space
preexecing git commit space
fatal: not a git repository (or any of the parent directories): .git
我的正则表达式似乎从来都不匹配。为什么?您的正则表达式不匹配,因为
[:space:
与[aceps:][/code>相同;与a
、c
、e
、p
、s
或中的一个匹配的字符类:
。你的意思可能是[:space:]
。以下几点应该有效
if [[ "$1" =~ ^[[:space:]]*git[[:space:]]+commit[[:space:]] ]]
然而,您当前的方法有点奇怪。你考虑过写一篇文章吗
即使很少有git钩子不是选项的情况,bash函数也会更好:
git(){
如果[“$1”=提交];则
#做一些事情,例如使用'set'插入参数--`
其他的
#在任何情况下,运行真正的git
命令git“$@”
fi
}
预提交git钩子的参数是什么?我在$@
中看不到任何参数,无论我如何git提交
,所以我看不到如何包装它…我想这样做是的,似乎你无法检查预提交
钩子中的命令行参数。在这种情况下,shell函数会更好。然而,如果你总是想找作者,你为什么要这么做?我不知道如何在每次预提交之前设置作者和git提交者。我将只使用git()
wrapper函数