BASH, multiple arrays and a loop
At work, we have 7 or 8 hardrives we dispatch over the country, each have unique labels which are not sequential.
Ideally drives are plugged in our desktop, then gets folders from the server that correspond to the drive name.
Sometimes, only one hard drive gets plugged in sometimes multiples, possibly in the future more will be added.
Each is mounted to /Volumes/ and its identifier; so for example /Volumes/f00, where f00 is the identifier.
What I want to happen, scan volumes see if any any of the drives are plugged in, then check the server to see if the folder exists, if it does copy folder and recursive folders.
Here is what I have so far, it checks if the drive exists in Volumes:
#!/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}]}"
开发者_如何学JAVA if [ -d "$dir" ]; then
echo "$dir exists, you win."
else
echo "$dir is not attached."
fi
done
What I can't figure out how to do, is how to check the volumes for the server while looping through the harddrive mount points.
So I could do something like:
#!/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
I know, that is pretty wrong... well very, but I hope it gives you the idea of what I am trying to achieve.
Any ideas or suggestions?
Some notes:
- You're using variables before you define them (I see that you set this variable a second time in a more appropriate place) and those quotes are unnecessary:
ARRAY1=($(ls "$BaseDir/$ServerMount/$Dispatch"))
- Missing closing quote:
ServerMount="BigBlue"
- Using curly braces when they're not necessary hinders readability (you could also omit the dollar sign for array subscripts):
dir="$BaseDir/${ARRAY[i]}"
- This array isn't defined:
echo ${list[@]}
- Huh? I think you may want nested for loops and you have to use different variables. Instead of:
for (( i=0;i<$DRIVES;i++)); (( i=0;i<$SERVERFOLDER;i++));
try:
for (( i= ... do for (( j= ... do dir="$BaseDir/${ARRAY[i]}" ser="${ARRAY1[j]}"
- You should be aware that if the filenames or directory names have spaces in them, iterating over an array will fail. The alternative is to pipe
find
into awhile...read
loop. - Here you call it "ser" and then you call it "sir":
ser="${ARRAY1[i]}"
You appear to be confused about nested for
loops, quite independent of arrays. The first trick is not to use the same index variable for both loops, and the second is to interleave the keywords and the enumerations properly, like this:
for x in a b c; do
for y in 1 2 3; do
echo $x$y
done
done
This prints:
a1
a2
a3
b1
b2
b3
c1
c2
c3
精彩评论