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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
latech
  • 73
  • 1
  • 10
  • Possible duplicate of [What do $? $0 $1 $2 mean in shell script?](https://stackoverflow.com/q/29258603/608639), [What are the special dollar sign shell variables?](https://stackoverflow.com/q/5163144), etc. Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Oct 16 '18 at 04:48
  • 1
    @jww: One could argue that's the case, but OP clearly knows what `$1`, `$2` are, but not know how to shift args. A dupe involving `shift` would be more appropriate here – Inian Oct 16 '18 at 04:50
  • The question looks so similar to your last one - [bash/shell script not reading second argument](https://stackoverflow.com/q/52818885/5291015) – Inian Oct 16 '18 at 04:55
  • 1
    @Inian - Yes, agreed. [Grzegorz's answer](https://stackoverflow.com/a/29258643/608639) looked pretty good because it talked about how arguments can move around in seemingly unexpected ways. – jww Oct 16 '18 at 04:55

2 Answers2

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.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • 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 – latech Oct 16 '18 at 14:05
  • Hi, I was merely answering the parameters problem. As for git itself, I do not know git enough to provide a definitive answer. – Nic3500 Oct 17 '18 at 11:37
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].

pjh
  • 6,388
  • 2
  • 16
  • 17