终端在尝试进行git提交时意外运行命令

cod*_*321 1 python git shell ubuntu

我们正在尝试编写一个基于提交历史记录发布文章的脚本.我们运行以下命令而不是进行提交,它实际上将我们放入了一个python终端;

cchilders:~/blogplish (master) 
$ git commit -m "You can run this file using the `python` command in your terminal:\n```$ python blogplish.py\nThe script is working.```"
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Run Code Online (Sandbox Code Playgroud)

我们很好奇它为什么这样做,上面命令中的两个蟒蛇中的哪一个引起了这个问题.另外,当我们提交这样的提交时,我们如何防止这种情况.我们的脚本将生成的目标语言是markdown.谢谢!

tor*_*rek 7

这与Python本身,Git以及与shell(命令行解释器)有关的一切都无关.

大多数Unix-ish shell共享一些语法特性:

  • 单引号是"强引号",可以防止几乎所有或所有扩展,并且仅以另一个单引号结束.
  • 双引号是"弱引号",防止一些扩展,但特别允许变量值替换像$var和反引号扩展`command`.但请注意,双引号引用单引号,因此"he said 'hello'"保留内部单引号.

在这种情况下,:

"... `python` ..."
Run Code Online (Sandbox Code Playgroud)

部分告诉你的shell运行python,看看它的标准输出产生了什么.无论那是什么,都会在那时取而代之.

(这种反引用扩展很难使用.如果你想要它,通常最好使用它$(...),因为括号帮助人类弄清楚命令中的内容,并且嵌套变得明显:reprocess $(process --files $(ls))意味着:

  • ls
  • 使用其输出作为参数,运行 process --files
  • 使用其输出作为参数,运行 reprocess

如果ls打印README hello.txt,这就像跑步process --files README hello.txt.无论process打印什么,然后拼接到参数reprocess.)

要完成这里的项目,首先所有反引号的表达式将按顺序运行和拼接.有四对反引号:

`python`
``
`$ python blogplish.py\nThe script is working.`
``
Run Code Online (Sandbox Code Playgroud)

其中两个是空的,所以它们什么都不运行,什么也没有拼接.一个运行python,最后一个运行$,可能会产生:

$: not found
Run Code Online (Sandbox Code Playgroud)

但是在shell到达那一点之前,它必须首先完成第一个python(然后运行空命令).

最后,正如其他人提到的,你可以在没有-m,或使用更强的引用变体-m.还有一个选择,为此我们需要git commit文件:承诺可以运行-F <file>--file=<file>从准备好的文件中读取消息.您甚至可以使用-F -从标准输入读取消息:

printf '%s\n\n%s\n' 'commit subject' 'commit message body' | git commit -F -
Run Code Online (Sandbox Code Playgroud)

例如.