Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass all command line arguments of my bash script as one argument to another program?

Tags:

git

bash

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

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?

like image 251
pqn Avatar asked Jul 24 '11 18:07

pqn


2 Answers

The quickest answer here is that in your script, the commit line should read

git commit -m "$*"

like image 145
jdd Avatar answered Oct 20 '22 01:10

jdd


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.

like image 27
lzap Avatar answered Oct 20 '22 01:10

lzap