Arrays Bash中的嵌套数组来自分离数组值的集合
我有多个项目,其中一个是专门的部署配置,用于设置多个存储库,从文件和文件夹配置服务器结构,最后部署一些服务(基于Docker) 通过这一个项目,我想知道如何解决以下情况:Arrays Bash中的嵌套数组来自分离数组值的集合,arrays,linux,bash,git,loops,Arrays,Linux,Bash,Git,Loops,我有多个项目,其中一个是专门的部署配置,用于设置多个存储库,从文件和文件夹配置服务器结构,最后部署一些服务(基于Docker) 通过这一个项目,我想知道如何解决以下情况: 从一组(我称之为标签)开始 folder表示将从Git克隆存储库的源位置(我在不同类型的“文件夹”中有成对的存储库名称) repository是Git中存储库的名称 branch是将在脚本中使用的Git的远程分支 因此,为了使用数组中的值,它们需要分开。我就是这样做的: for collection in ${REPO
- 从一组(我称之为标签)开始
- folder表示将从Git克隆存储库的源位置(我在不同类型的“文件夹”中有成对的存储库名称)
- repository是Git中存储库的名称
- branch是将在脚本中使用的Git的远程分支
for collection in ${REPOSITORY_COLLECTION[@]}; do
folder="${collection%%:*}" # only take the first part
result="${collection##"$folder:"}" # remove folder from collection value
repository="${result%%:*}" # only take the second part
branch="${result#"$repository:"}" # only take the third part
# then, add it to new arrays to keep them separated and for the requirement that it is going to be used within the other functionalities of the script
repository_tags+=("$collection")
repository_types+=("$folder")
repositories+=("$repository")
repository_branches+=("$branch")
done
之后,我将需要“$repository\u type”和“$repositories”来检查该存储库是否已经存在。所以我需要塑造一条道路;但如果我没有错的话,它将要求我仍然将所有分离的值与$collection“$repository_tags”的源进行比较,以检查两者的组合是否正确。值来自同一源数组,但根据彼此随机选择。因此:
for tags in ${repository_tags[@]}; do
for repository_type in ${repository_types[@]}; do
# beginning with the source
source_path="/path_to_server/repositories/$repository_type"
if [ -d "$source_path" ]; then
echo && echo "$source_path exist"
else
echo && echo "$source_path doesn't exist, creating it.."
echo "mkdir -p $source_path"
mkdir -p $source_path
fi
for repository in ${repositories[@]}; do
if [[ "$tags" == "$repository_type"* ]] && [[ "$tags" == *"$repository"* ]]; then
# adding the repository value to it, and check if it exists
if [ ! -d "$source_path/$repository" ]; then
# clone from Git
echo "Source '$source_path': Ready to clone $repository"
else
# pull changes from Git
echo "Source '$source_path': Ready to pull changes from $repository"
fi
fi
done
done
done
根据运行此命令时的结果:
/path_to_server/repositories/folder_a doesn't exist, creating it..
mkdir -p /path_to_server/repositories/folder_a
Source '/path_to_server/repositories/folder_a': Ready to clone repository_a
/path_to_server/repositories/folder_b doesn't exist, creating it..
mkdir -p /path_to_server/repositories/folder_b
/path_to_server/repositories/folder_c doesn't exist, creating it..
mkdir -p /path_to_server/repositories/folder_c
/path_to_server/repositories/folder_a exist
/path_to_server/repositories/folder_b exist
Source '/path_to_server/repositories/folder_b': Ready to clone repository_b
/path_to_server/repositories/folder_c exist
/path_to_server/repositories/folder_a exist
/path_to_server/repositories/folder_b exist
/path_to_server/repositories/folder_c exist
Source '/path_to_server/repositories/folder_c': Ready to clone repository_c
我喜欢并希望第一行重复每一个其他值,例如:
/path_to_server/repositories/folder_a doesn't exist, creating it..
mkdir -p /path_to_server/repositories/folder_a
Source '/path_to_server/repositories/folder_a': Ready to clone repository_a
有人知道我在这种情况下能做得更好吗,或者知道如何解决这个问题吗?IFS=:read-r folder repo branch
/path_to_server/repositories/folder_a doesn't exist, creating it..
mkdir -p /path_to_server/repositories/folder_a
Source '/path_to_server/repositories/folder_a': Ready to clone repository_a