Hi, I have cobbled together a simple script to create a Windows folder in a bunch of home folders on a mac server using the following code.
for i in /Volumes/student_data/studenthomefolders/*
do
u=`echo $i | cut -d/ -f5`
//if [ -d $i/Windows ]
//then
//echo "Folder already exists for "$u" Skipping"
//else
echo "Making Windows Dir for "$u
mkdir $i/Windows
echo "Changing Ownership of Windows Directory for "$u
chown $u $i/Windows
echo "Setting Mode for Windows Folder for "$u
chmod 700 $i/Windows
done
The commented out part is my attempt to add a check to see if that folder already exists and to skip the creation and manipulation if it does but my scripting knowledge is limited and that code returns an error.
If I run the script after creating a new user it currently runs through all of them, fails to create the existing ones but still chmods and chowns them, while this is not a massive problem I would like a neater script, anyone point me in the right direction.
Preferably it would silently skip existing ones and just report the new creations.
How about changing the logic to create a list of all directories that don't already have the Windows folder. Then use that list in your for loop to create them?
for i in /Volumes/student_data/studenthomefolders/*
do
u=`echo $i | cut -d/ -f5`
if [ ! -d $i/Windows ]; then
echo "Making Windows Dir for "$u
mkdir $i/Windows
echo "Changing Ownership of Windows Directory for "$u
chown $u $i/Windows
echo "Setting Mode for Windows Folder for "$u
chmod 700 $i/Windows
fi
done
Many thanks, that diddn't quite work but altering it to
for i in /Volumes/student_data/studenthomefolders/*
do
u=`echo $i | cut -d/ -f5`
if [ -d $i/Windows ];
then
echo $u" Windows folder exists" >/dev/null
else
echo "Making Windows Dir for "$u
mkdir $i/Windows
echo "Changing Ownership of Windows Directory for "$u
chown $u $i/Windows
echo "Setting Mode for Windows Folder for "$u
chmod 700 $i/Windows
fi
done
worked like a charm, although I do get an error of too many arguments on line 4 but it still works so thanks. Is there a command that skips rather than a useless echo to null I can use?