latech
latech

Reputation: 73

bash/shell script not reading second argument

this is simple script which i want to read first and second argument somehow its not reading the second argument and throwing error stating pass the value.

Here is the script //I want to clone the git

$cat test.sh

#!/usr/bin/env bash

clone () {
  git clone $2
}

case $1
in
   clone) clone ;;

       *) echo "Invalid Argument passed" ;;
esac

Running the script

$./test.sh clone https://github.com/sameerxxxxx/test.git/
fatal: You must specify a repository to clone.

usage: git clone [<options>] [--] <repo> [<dir>]

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --progress            force progress reporting

Upvotes: 2

Views: 1195

Answers (2)

ElpieKay
ElpieKay

Reputation: 30858

#!/usr/bin/env bash

clone () {
  git clone $1
}

case $1
in
   clone) clone $2 ;;

       *) echo "Invalid Argument passed" ;;
esac

https://github.com/sameerxxxxx/test.git/ is the 2nd parameter passed to test.sh, so clone $2 ;; instead of clone ;;.

For clone $2, $2 is the 1st parameter passed to the function clone, so git clone $1 instead of git clone $2.

Upvotes: 0

kojiro
kojiro

Reputation: 77069

When you call your function clone, you have to pass the arguments to it.

clone() {
    git clone "$1"
}
...
clone) clone "$2";;

Note that the function's positional parameters are numbered separately from the script itself.

Upvotes: 2

Related Questions