How can I pass all command line arguments of my bash script as one argument to another program?
I wish to write a simple git script that will run the following lines:
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
开发者_JAVA百科I'm new to bash scripting, so this has been a bit of a problem for me. My existing attempt is as follows:
#!/bin/sh
cd <my repo name which has no have any spaces>
git add *
git add -u
git commit -m $*
git push origin master
I don't quite know how to throw in a proper string argument surrounded by quotes. I currently try to run the program like this:
autogit.sh "Example comment."
How do I have to change my script so it works with multi-word commit comments?
The quickest answer here is that in your script, the commit line should read
git commit -m "$*"
Here are few examples of my git aliases that could help you. I am doing similar things.
http://lukas.zapletalovi.com/2011/04/my-git-aliases.html
For example:
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
...
It takes one parameter, also all commands are concatenated with && which stops with error code 1 immediately if any command in the chain (e.g. merge) fails. Good luck with aliases.
Bash does string interpolation. Your script should be fine if you replace the line
git commit -m $*
with
git commit -m "$*"
git commit -m "$*"
should do it for you.
精彩评论