BASH, multiple arrays and a loop
At work, we have 7 or 8 hard drives that we ship across the country, each with unique tags that are not sequential.
Ideally, drives connect to our desktop and then get folders from the server that match the drive's name.
Sometimes, only one hard drive is connected sometimes to multiples, perhaps more will be added in the future.
Each is mounted in / Volumes / and its identifier; for example, / Volumes / f 00, where f00 is an identifier.
What I want is it scans volumes if any of the mapped drives are mapped, then check the server to see if the folder exists if it copies the folder and recursive folders.
Here is what I have checked so far 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}]}"
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 this is how to validate volumes for the server, poking through the hard drive 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 this is pretty wrong ... well, very much, but I hope it gives you an idea of ββwhat I am trying to achieve.
Any ideas or suggestions?
a source to share
Some notes:
- You use variables before defining them (I see that you are setting this variable a second time in a more appropriate place) and these quotes are unnecessary:
ARRAY1=($(ls "$BaseDir/$ServerMount/$Dispatch"))
- Missing final quote:
ServerMount="BigBlue"
- Using curly braces when not needed makes readability difficult (you can also omit the dollar sign for array indices):
dir="$BaseDir/${ARRAY[i]}"
- This array is undefined:
echo ${list[@]}
- AND? I think you may want nested loops and 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 filenames or directory names have spaces in them, then iterating over the array will fail. An alternative is to pipe
find
to cyclewhile...read
. - Here you call it "ser" and then you call it "sir":
ser="${ARRAY1[i]}"
a source to share
It looks like you are confused about nested loops for
completely independent of arrays. The first trick is not to use the same index variable for both loops, and the second is to alternate keywords and enums correctly, like so:
for x in a b c; do
for y in 1 2 3; do
echo $x$y
done
done
Prints:
a1
a2
a3
b1
b2
b3
c1
c2
c3
a source to share