2017-08-10 66 views
0

我想要做的是拥有一个控制非常基本的备份脚本的配置文件。BASH在远程配置文件中具有相同名称的多个变量

#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循环for循环会回显最后的源和目标是正常的,因为它不知道什么时候为“n = 1”完成某一组变量。

你们会怎么做?

非常感谢!

+0

请使用'源config.ini',而不是'$出口(猫的config.ini | grep的-v ^#| xargs的)' – hek2mgl

+0

谢谢你的提示。 :) –

+0

:)怎么样使用'json'的配置文件?这不会简化很多东西吗? – hek2mgl

回答

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 
+0

感谢您的快速回复! –

+0

问题是,我想为每个“n”对象在config.ini(源和目标)中定义一些参数。可能吗? –

相关问题