2011-06-28 59 views
6

我有一系列命令在运行git项目之前运行,因此我将它放在bash脚本中。最后我有一个做了块提交:bash:传递脚本参数

if [ -z $1 ]; then git commit -a -m "no message"; else; git commit -a -m $1; fi 

,并期望该消息被传递给脚本

$ ./dostuff_then_commit "my message" 

当我这样做,我得到

fatal: Paths with -a does not make sense. 

因为$1已被定义,但消息没有正确传递?任何人都可以看到问题和/或提出解决方案吗?感谢所以。

回答

6

如果邮件包含空格,它将扩展为多个参数至git commit。 (请注意,在其他情况下的报价。)引用它:

if [ -z "$1" ]; then 
    git commit -a -m "no message" 
else 
    git commit -a -m "$1" 
fi 

一对夫妇补遗:

  • 我还引用了一个在[],一个稍微不同的理由:如果提交信息是空的,你会从[得到缺失的参数诊断。再次引用它避免了这一点。 (你可能想要捕捉它,并让用户输入一个真正的提交信息,但如果这是必要的,你可能会得到一堆asdfzxcv提交信息....)

  • 错误消息,重新获取是因为提交消息的第一个单词被视为提交消息,其余的被作为特定文件名传递提交;正如错误信息所述,这告诉git承诺一切(-a)是没有意义的。

+0

感谢有额外位。 – hatmatrix

2

尝试围绕$1加上引号 - 否则混帐认为my是消息和message是另一回事。

if [ -z $1 ]; then git commit -a -m "no message"; else; git commit -a -m "$1"; fi 
1

你应该使用的"$1"代替$1 as $ 1`可以有空格在里面。

$1my message代入:

git commit -a -m $1 

给出:

git commit -a -m my message 

而:

git commit -a -m "$1" 

给出:

git commit -a -m "my message" 
2

我只想补充一点,你可以结合选项,如下所示:

git commit -am "some message" 
+0

啊,谢谢..... – hatmatrix