Bash 使用shell脚本克隆多个git存储库

Bash 使用shell脚本克隆多个git存储库,bash,shell,Bash,Shell,我想同时克隆几个git存储库。但我的脚本似乎工作不正常 #!/bin/sh declare -a git_resources=("https://git.polarsys.org/c/capella/capella.git" "https://github.com/mbats/mindstorms") if [ ! -d "$HOME"/git ]; then mkdir "$HOME"/git fi cd "$HOME"/git || { printf "cd failed,

我想同时克隆几个git存储库。但我的脚本似乎工作不正常

#!/bin/sh

declare -a git_resources=("https://git.polarsys.org/c/capella/capella.git"  "https://github.com/mbats/mindstorms")

if [ ! -d "$HOME"/git ]; then
    mkdir "$HOME"/git
fi

cd "$HOME"/git || { printf "cd failed, exiting\n" >&2;  return 1; }

for i in ${git_resources[@]} ; do
 echo  $i


done
返回的是:
语法错误:(“意外的

我如何声明字符串列表有什么问题吗

这是一个sh脚本。sh没有数组或
declare
builtin。因为您想在bash中编写,所以在脚本开始时使用

#!/bin/bash

不要单独处理每一个错误——并且只有在您记得这样做的时候——而是将
set-e
放在脚本的开头,以便在出现错误时停止


除非您知道为什么需要省略它们。这即使在扩展数组时也适用:
${git_resources[@]}
拆分和全局单个数组元素。您需要
“${git_resources[@]}”
来获取数组元素列表



问题出在哪里?请发布预期结果。主要问题:您的脚本没有尝试克隆任何git Respositions。次要问题:这样使用
return
将失败,即使git目录不存在,for循环也将执行。“工作不正常”是什么意思?我的意思是不返回预期的结果。我鼓励您将问题缩小到更小的范围。也许只需声明字符串列表,而不对它们做任何操作。在介绍git或目录之前,请先看看是否可以使其正常工作。
#!/bin/bash
cd "$HOME"/git || …
for i in ${git_resources[@]} ; do
 echo  $i
#!/bin/bash

set -e

declare -a git_resources=(
    "https://git.polarsys.org/c/capella/capella.git"  
    "https://github.com/mbats/mindstorms"
)

if [ ! -d "$HOME"/git ]; then
    mkdir "$HOME"/git
fi
cd "$HOME"/git

for url in "${git_resources[@]}"; do
    dir="${url##*/}"
    dir="${dir%.git}"
    if [ -e "$dir/.git" ]; then
        git -C "$dir" update
    else
        git clone "$url"
    fi
done