2010-04-30 82 views
2

在工作中,我们有7或8个派往全国各地的硬盘驱动器,每个硬盘驱动器都有唯一的不连续的标签。BASH,多个阵列和一个循环

理想情况下,驱动器插在我们的桌面上,然后从服务器获取与驱动器名称对应的文件夹。

有时,只有一个硬盘驱动器有时会插入倍数,未来可能会增加更多。


每个都安装到/卷/及其标识符;例如/ Volumes/f00,其中f00是标识符。

我想要发生的情况是扫描卷,看是否有任何驱动器插入,然后检查服务器以查看文件夹是否存在,是否存在复制文件夹和递归文件夹。


这里是我迄今为止,它会检查驱动器卷上存在:

#!/bin/sh 

#Declare drives in the array 

ARRAY=(foo bar long) 


#Get the drives from the array 
DRIVES=${#ARRAY[@]} 

#Define base dir to check 
BaseDir="/Volumes" 


#Define shared server fold on local mount points 
#I plan to use AFP eventually, but for the sake of ease 
#using a local mount. 
ServerMount="BigBlue" 

#Define folder name for where files are to come from 
Dispatch="File-Dispatch" 

dir="$BaseDir/${ARRAY[${i}]}" 


#Loop through each item in the array and check if exists on /Volumes 
for ((i=0;i<$DRIVES;i++)); 
do 
    dir="$BaseDir/${ARRAY[${i}]}" 
    if [ -d "$dir" ]; then 
    echo "$dir exists, you win." 
    else 
    echo "$dir is not attached." 
    fi 
done 

我想不出该怎么办,是怎么检查在循环硬盘安装点时服务器的卷。

所以我可以做类似:

#!/bin/sh 

#Declare drives, and folder location in arrays 

ARRAY=(foo bar long) 



#Get the drives from the array 

DRIVES=${#ARRAY[@]} 


#Define base dir to check 
BaseDir="/Volumes" 

#Define shared server fold on local mount points 

ServerMount="BigBlue 

#Define folder name for where files are to come from 
Dispatch="File-Dispatch" 

dir="$BaseDir/${ARRAY[${i}]}" 

#List the contents from server directory into array 


ARRAY1=($(ls ""$BaseDir"/"$ServerMount"/"$Dispatch"")) 
SERVERFOLDER=${#ARRAY1[@]} 

echo ${list[@]} 

for ((i=0;i<$DRIVES;i++)); ((i=0;i<$SERVERFOLDER;i++)); 
do 
    dir="$BaseDir/${ARRAY[${i}]}" 
    ser="${ARRAY1[${i}]}" 
    if [ "$dir" =~ "$sir" ]; then 
    cp "$sir" "$dir" 
    else 
    echo "$dir is not attached." 
    fi 
done 

我知道,这是非常错误的...非常好,但我希望它给你什么,我想实现的想法。

任何想法或建议吗?

回答

1

的一些注意事项:

  • 您使用的变量定义之前,他们(我看到你设置此变量,第二次在一个更合适的地方)和这些报价是不必要的:
    ARRAY1=($(ls "$BaseDir/$ServerMount/$Dispatch"))
  • 缺少收盘报价:
    ServerMount="BigBlue"
  • 使用大括号时,他们没有必要阻碍可读性(你也可以省略了数组下标,美元符号):
    dir="$BaseDir/${ARRAY[i]}"
  • 此数组不定义如下:
    echo ${list[@]}
  • 咦?我想你可能想嵌套for循环,你必须使用不同的变量。相反的:
    for ((i=0;i<$DRIVES;i++)); ((i=0;i<$SERVERFOLDER;i++));
    尝试:
 for ((i= ... 
    do 
     for ((j= ... 
     do 
      dir="$BaseDir/${ARRAY[i]}" 
      ser="${ARRAY1[j]}"
  • 你应该知道,如果文件名或目录名有空格的,迭代数组将失败。另一种方法是将find转换为while...read循环。
  • 在这里,你把它称为“SER”,然后你把它称为“先生”:
    ser="${ARRAY1[i]}"
+0

对不起,这些错误中的一半来自于我懒惰复制到这里和编辑时,但谢谢我也会试试这个。我知道((i = 0; i <$ DRIVES; i ++)); ((i = 0; i <$ SERVERFOLDER; i ++));是不正确的,但这是我想要的。 你在上个月帮我解决了一些问题,我非常感谢。 – S1syphus 2010-04-30 15:21:45

1

您似乎对嵌套的for循环感到困惑,它完全独立于数组。第一招是不使用相同的索引变量两个回路,第二是交错的关键字和枚举正确,就像这样:

for x in a b c; do 
    for y in 1 2 3; do 
    echo $x$y 
    done 
done 

此打印:

a1 
a2 
a3 
b1 
b2 
b3 
c1 
c2 
c3 
+0

将 http://pastebin.com/F0RPrbAW 更合适的话,对不起,还没有真正之前做过。 – S1syphus 2010-04-30 15:17:14

+0

那么这似乎工作,只是匹配变量多数民众赞成在这个问题上,但我相信谷歌会排序一个。 – S1syphus 2010-04-30 15:19:47

+0

@ S1syphus:关于你的pastebin代码,你必须为两个循环(和两个数组索引)使用不同的变量。 – 2010-04-30 15:42:27