庆典:通过脚本参数 [英] bash: passing script arguments

查看:117
本文介绍了庆典:通过脚本参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的我,所以我已经把它放在一个bash脚本犯了Git项目之前运行的命令。最后我有一个确实的承诺块:

I have a series of commands that I run before committing a git project so I've put it in a bash script. At the end I have a block that does the commit:

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

与期望该消息被传递给脚本

with the expectation that the message is passed to the script

$ ./dostuff_then_commit "my message"

当我这样做,我得到

fatal: Paths with -a does not make sense.

由于 $ 1 已定义,但没有正确传递的信息?任何人都可以看到的问题和/或建议的解决方案?感谢这么

because $1 has been defined but the message is not passed correctly? Can anyone see the problem and/or suggest a solution? Thanks SO.

推荐答案

如果消息包含空格,将扩大到多个参数 git的承诺。 (注意:在其他情况下的报价。)引用的:

If the message contains spaces, it will expand to multiple parameters to git commit. (Notice the quoting in the other case.) Quote it:

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

一对夫妇补遗:


  • 我还引用了一个在 [] ,一个稍微不同的理由:如果确认消息是空的,你会得到一个缺少的参数从诊断 [。再次,引用它避免了这一点。 (您可能不是想赶上那班,使用户进入一个真正的提交信息,但如果它是必要的,你很可能得到一堆 asdfzxcv 的提交信息.. ..)

  • I also quoted the one in the [], for a slightly different reason: if the commit message were empty, you would get a missing parameter diagnostic from [. Again, quoting it avoids this. (You might instead want to catch that and make the user enter a real commit message, although if it were that necessary you'd probably get a bunch of asdfzxcv commit messages....)

你得到该错误消息特别是因为提交消息的第一个字被作为提交信息,其余的具体文件名犯传递;这一点,作为错误消息状态,告诉混帐犯下的一切( -a )时,是没有意义的。

The error message you're getting is specifically because the first word of the commit message is taken as the commit message and the rest are passed as specific filenames to commit; this, as the error message states, makes no sense when telling git to commit everything (-a).

这篇关于庆典:通过脚本参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆