2017-08-10 60 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(源和目標)中定義一些參數。可能嗎? –

相關問題