bash / shell脚本不读取第二个参数

时间:2018-10-15 14:21:34

标签: shell unix

这是一个简单的脚本,我想以某种方式读取第一个和第二个参数,而不读取第二个参数,并抛出错误说明传递值。

这是脚本//我要克隆git

$cat test.sh

#!/usr/bin/env bash

clone () {
  git clone $2
}

case $1
in
   clone) clone ;;

       *) echo "Invalid Argument passed" ;;
esac

运行脚本

$./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 个答案:

答案 0 :(得分:2)

调用函数clone时,必须将参数传递给它。

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

请注意,函数的位置参数与脚本本身分开编号。

答案 1 :(得分: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/是传递给test.sh的第二个参数,因此clone $2 ;;而不是clone ;;

对于clone $2$2是传递给函数clone的第一个参数,因此git clone $1而不是git clone $2