如何编写一个shell脚本来克隆我的所有repo';来自gitlab

如何编写一个shell脚本来克隆我的所有repo';来自gitlab,git,shell,Git,Shell,问题是我想写一个简单的shell脚本,从我的内部gitlab克隆我的回购。我有点困惑的主要事情是如何循环和克隆每个项目,以及如何检查项目是否存在。这就是我到目前为止所得到的任何帮助都将不胜感激 gitlab_pull_script.sh #!/bin/bash BLUE='\033[0;34m' GREEN='\033[0;32m' NC='\033[0m' printf "${GREEN}Initializing starting sciprt${BLUE} Pulling from

问题是我想写一个简单的shell脚本,从我的内部gitlab克隆我的回购。我有点困惑的主要事情是如何循环和克隆每个项目,以及如何检查项目是否存在。这就是我到目前为止所得到的任何帮助都将不胜感激

gitlab_pull_script.sh

#!/bin/bash


BLUE='\033[0;34m'
GREEN='\033[0;32m'
NC='\033[0m' 

printf "${GREEN}Initializing starting sciprt${BLUE} Pulling from code.mydomain.com${NC}\n"

#var's
codeHost=mydomain.com
codeUser=admin
testApp1="git@$codeHost:$codeUser/testapp1.git"
testApp2="git@$codeHost:$codeUser/testapp2.git"
testApp3="git@$codeHost:$codeUser/testapp3.git"
testApp4="git@$codeHost:$codeUser/testapp4.git"
testApp5="git@$codeHost:$codeUser/testapp5.git"

#this is the part I'm not sure about for checking to make sure the project exist 
codeProjects="$tesApp1" && "$tesApp2" && "$tesApp3" && "$tesApp4" && "$tesApp5"


localCodeDir="${HOME}/code/"


do 
   #once again im not sure if this works to check $codeprojects variable  if it exist or not. 
   if [ -z "$codeProjects" ]; then
      echo "Project not found"
      exit
   else # this is where I am confused on how to make loop through each project ********
      cloneCmd="git clone $testApp1"
      cloneCmd="git clone $testApp2"
      cloneCmd="git clone $testApp3"
      cloneCmd="git clone $testApp4"
      cloneCmd="git clone $testApp5"
   fi
done
像这样的

for app in testapp1 testapp2 testapp3 foo bar emacs-snapshot; do
    git clone "git@$codeHost:$codeUser/$app.git"
done
如果您绝对希望将项目放在变量中,那么将它们指定给数组(甚至是简单的以空格分隔的带引号的字符串)就可以了

apps=(testapp1 testapp2 testapp3 foo bar emacs-snapshot)
for app in "${apps[@]}"; do
    :
甚至

apps="testapp1 testapp2 testapp3 foo bar emacs-snapshot"
for app in $apps; do
    :
我不必担心检查项目是否存在--
git
会抛出一个错误并跳过该项目,循环将继续执行列表中的下一个项目

for app in testapp1 testapp2 testapp3 foo bar emacs-snapshot; do
    git clone "git@$codeHost:$codeUser/$app.git"
done
如果您绝对希望将项目放在变量中,那么将它们指定给数组(甚至是简单的以空格分隔的带引号的字符串)就可以了

apps=(testapp1 testapp2 testapp3 foo bar emacs-snapshot)
for app in "${apps[@]}"; do
    :
甚至

apps="testapp1 testapp2 testapp3 foo bar emacs-snapshot"
for app in $apps; do
    :

我不担心检查项目是否存在--
git
将抛出一个错误并跳过该项目,循环将继续执行列表中的下一个项目。

此工具只需最少的设置即可满足您的需要:


需要安装Java和Gradle。

此工具只需最少的设置即可满足您的需要:


需要安装Java和Gradle。

Git Pull和Git Clone之间存在差异。你想做哪一个呢?Git clone,我在考虑用一个单独的脚本来做pull,并把它作为cron的工作@user2984552b获取项目列表会稍微有点困难,但我认为gitlab提供了一个API来枚举项目。但是,如果您满足于将项目列表硬编码到脚本中,这一点也不难。Git Pull和Git Clone之间有区别。你想做哪一个呢?Git clone,我在考虑用一个单独的脚本来做pull,并把它作为cron的工作@user2984552b获取项目列表会稍微有点困难,但我认为gitlab提供了一个API来枚举项目。但是,如果您满足于将项目列表硬编码到脚本中,这一点也不难。第三个建议效果最好,脚本可以运行,但每次它克隆一个项目时都需要我的pub.key。我如何才能将其放入,使其成为一个自动脚本?因为当我输入密码时,它说我无法访问这个repo,这是没有意义的,因为如果我手动复制git,它就会工作。有什么想法吗@这是一个单独的问题,也是一个大量的常见问题。第三个建议效果最好,是脚本运行,但是每次它克隆一个项目时,它都需要我的pub.key。我怎么能把它放进去,使它成为一个自动脚本?因为当我输入密码时,它说我无法访问这个repo,这是没有意义的,因为如果我手动复制git,它就会工作。有什么想法吗@这是一个单独的问题,也是一个大量的常见问题。谷歌一点。