The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if file exists in a mounted windows shared folder jul Shell Programming and Scripting 2 05-14-2008 01:50 AM
How to check if a direcorty exists? SunnyK Shell Programming and Scripting 2 11-08-2007 10:47 AM
check if directory exists jerardfjay Shell Programming and Scripting 2 06-13-2005 02:26 PM
Check Remote Folder Exists borncrazy Shell Programming and Scripting 1 07-12-2004 04:15 PM
Folder Exists borncrazy UNIX for Dummies Questions & Answers 5 06-19-2004 01:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-26-2007
psyman psyman is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 3
simple check to see if a folder exists

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.

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.

Many thanks in advance
  #2 (permalink)  
Old 09-26-2007
tomas's Avatar
tomas tomas is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 35
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?
  #3 (permalink)  
Old 09-26-2007
[MA]Flying_Meat [MA]Flying_Meat is offline
Registered User
  
 

Join Date: Sep 2002
Location: San Fran
Posts: 130
Code:
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
  #4 (permalink)  
Old 09-27-2007
psyman psyman is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 3
Many thanks, that diddn't quite work but altering it to

Code:
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?
  #5 (permalink)  
Old 09-27-2007
[MA]Flying_Meat [MA]Flying_Meat is offline
Registered User
  
 

Join Date: Sep 2002
Location: San Fran
Posts: 130
that's sorta odd...

If the directory doesn't exist, then do the stuff.

Pretty simple test.

What OS/shell version are you using?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0