0

I need to pass argument for commit function. When I do the commit through

./test.sh commit -m "first" 

its not really committing it. believe somehow I am not passing right argument parameter either in case or function.

Here is the script

#!/usr/bin/env bash

clone () {
  git clone $1
}

commit () {
  git commit $*
}

case $1
in
   clone) clone $2 ;;
   commit) commit $2 ;;

       *) echo "Invalid Argument passed" ;;
esac
4

2 Answers 2

1

The arguments are processed like this by bash:

./test.sh commit -m "first" 

0: ./test.sh
1: commit
2: -m
3: first

So your "first" is actually argument $3.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for all..really appreciate your help. so my code looks seems ok ? But somehow its not working yet. strangely i have to run twice add and commit to make this works, i believe i missing something. commit () { git commit $1 $2 } case $1 in clone) clone $2 ;; commit) commit $2 $3 ;; *) echo "Invalid Argument passed" ;; esac
Hi, I was merely answering the parameters problem. As for git itself, I do not know git enough to provide a definitive answer.
1

To safely support multiple arguments (including ones with special characters) the function bodies should be

git clone "$@"

and

git commit "$@"

.

For the same reasons, the case code should be:

case $1 in
    clone)  clone "${@:2}" ;;
    commit) commit "${@:2}" ;;
    *)      echo "Invalid Argument passed" ;;
esac

In the functions, "$@" expands to all the function arguments, safely quoted so they are not subject to word splitting or expansions.

In the case statement, ${@:2} expands to the list of command line arguments after the first one, safely quoted.

For more information see Handling positional parameters [Bash Hackers Wiki].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.