BASH远程配置文件中具有相同名称的多个变量

时间:2017-08-10 09:38:45

标签: bash file variables config external

我要做的是拥有一个控制非常基本的备份脚本的配置文件。

#Credentials
username="backup.user"
password="****"
from="/mnt"
to="/home/backup"



#Mountpoints
n=1
source="//10.X.X.X/Public"
destination="/mnt/Public"

n=2
source="//10.X.X.X/it"
destination="/mnt/it"

在脚本本身内部看起来像这样:

#!/bin/bash
#getting variables from the external config file
export $(cat config.ini | grep -v ^# | xargs)

#the command I am trying to achieve
mountpoint[$n]="mount -t cifs -o username=$username,password=$password,ro $source $destination"

#mounts the array of mountpoints defined
for mountpoint in "${mountpoint[@]}";
        do
                ${mountpoint}
        done


function currentDate () {
date +%Y%m%d
}


if [ ! -d "$to/$(currentDate)" ] ; then
                mkdir "$to/$(currentDate)";
                cp --verbose -R "$from/." "$to/$(currentDate)" >> $to/$(currentDate)/fileLog.txt
                diff -qr $from $to/$(currentDate) >> $to/$(currentDate)/differencesLog.txt
else
                exit
fi
umount -a -t cifs -l /mnt/*
done

我想这样做: 在每个挂载点的配置中都有一组变量。

for循环将正常回显最后一个源和目标,因为它不知道何时为" n = 1"完成了一组变量。

你们将如何做到这一点?

非常感谢!

1 个答案:

答案 0 :(得分:0)

# create an array
mountpoint=()
# append to the array
mountpoint+=("item 1")
mountpoint+=("item 2")
# iterate the array
for i in "${mountpoint[@]}"; do
    echo "${i}"
done