2

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
2
  • Your script calls the shell function clone() without arguments... Commented Oct 15, 2018 at 14:23
  • And to add to alexis' comment, the argument $2 within your function refer to the arguments passed to the function, not to the script. Commented Oct 15, 2018 at 14:40

2 Answers 2

2

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.

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

2 Comments

@latech, always, always parenthesize those $variable when you're programming in the shell: if the variable contains a string with a space in it, using $variable when calling a function/command will produce as many arguments to the calls as there are spaces in the string, plus one.
@latech, also consider using set -u early in your scripts: should you have used it, the script would blow up in an attempt to call clone $2 with an undefined variable 2—supposedly hinting you at the root cause. All in all, looks like you did not bother to follow at least some basic tutorial on shell scripting, and just doing copying-and-pasting from SO. Please reconsider your approach to problem solving.
0
#!/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.

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.