如何将我的bash脚本的所有命令行参数作为一个参数传递给另一个程序?

时间:2011-07-24 18:40:31

标签: git bash

我希望编写一个简单的git脚本,它将运行以下行:

cd <the name of my git repo>
git add *
git add -u
git commit -m "<my comment in the form of a string>"
git push origin master

我是bash脚本的新手,所以这对我来说有点问题。我现有的尝试如下:

#!/bin/sh

cd <my repo name which has no have any spaces>
git add *
git add -u
git commit -m $*
git push origin master

我不太知道如何使用引号括起来引入正确的字符串参数。我目前尝试运行这样的程序:

autogit.sh "Example comment."

如何更改我的脚本以使其适用于多字提交注释?

4 个答案:

答案 0 :(得分:5)

这里最快的答案是在你的脚本中,提交行应该是

git commit -m "$*"

答案 1 :(得分:3)

以下是我的git别名可以帮助你的几个例子。我正在做类似的事情。

http://lukas.zapletalovi.com/2011/04/my-git-aliases.html

例如:

rem = !sh -c 'test "$#" = 1 && git h && git checkout master && git pull && git checkout \"$1\" && git rebase master && git checkout master && git merge \"$1\" && echo Done and ready to do: git pom && exit 0 || echo \"usage: git rem \" >&2 && exit 1' -

# git rem
usage: git rem ...

# git rem my_branch
...

它需要一个参数,所有命令都与&amp;&amp;和如果链中的任何命令(例如合并)失败,它会立即停止并显示错误代码1。祝你好运。

答案 2 :(得分:3)

Bash执行字符串插值。如果替换

行,您的脚本应该没问题
git commit -m $*

git commit -m "$*"

答案 3 :(得分:2)

git commit -m "$*"

应该为你做。

相关问题