BASH, multiple arrays and a loop.
- by S1syphus
At work, we 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 mounts to /Volumes/ and it's 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 checks the server to see if the folder exists, if ir 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}]}"
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 )
ARRAY1=($(ls ""$BaseDir"/"$ServerMount"/"$Dispatch""))
#Get the drives from the array
DRIVES=${#ARRAY[@]}
SERVERFOLDER=${#ARRAY1[@]}
#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""))
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?