将参数传递给linux shell从git仓库克隆

问题描述

我有一个script.sh,它执行git存储库的“克隆”操作。当前,该脚本可以正确地在相应行上用手写的目标分支正常工作。

/usr/bin/git clone https://my-url-repository --branch master --single-branch

如果我将“ master”替换为“ $ 1”,

/usr/bin/git clone https://my-url-repository --branch $1 --single-branch

由于无法正确执行分支并引发致命错误,因此无法完成操作。

warning: Could not find remote branch e to clone.
fatal: Remote branch e not found in upstream origin

但是,如果我在屏幕上打印参数$ 1或$ {1},则输入的值将正确显示

#!/bin/bash

if [ -n "$1" ]; then
    echo Building from branch "["$1"]"
else
    echo "Please,enter branch name"
    exit
fi

你能帮我吗?

解决方法

我无法具体解决问题,但确实找到了同样有效的替代方法。在同一脚本内,我请求输入分支名称,而不是等待脚本调用中的参数,然后完成 clone 而不出现问题。

代码如下:

#!/bin/bash

echo "Please,enter branch name"
read branchName

然后我使用这样的变量:

/usr/bin/git clone https://my-url-repository  --branch $branchName --single-branch