The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Backing up the system eykyn17 SUN Solaris 6 03-28-2007 12:49 PM
Can usfdump be used for backing up 1 directory? FredSmith SUN Solaris 5 01-12-2007 01:31 PM
backing up the vtoc BG_JrAdmin UNIX for Dummies Questions & Answers 1 12-22-2005 08:33 PM
Best practises for backing up d11wtq UNIX for Dummies Questions & Answers 2 07-06-2005 09:11 PM
backing up merlin UNIX for Dummies Questions & Answers 4 09-09-2001 05:35 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-17-2003
indigoecho indigoecho is offline
Registered User
  
 

Join Date: Dec 2003
Posts: 4
Backing Up Directories

Hi Guys,

I'm writing a shell script that presents the user with various options, they select one (numbered 1-9) and it then excecutes the correct code. No problem, but I'm having slight difficulty with one option.

The user can select to backup all the files in the current directory to another directory, but the program must first check to see if the destination directory exists and then if not it must create it before copying the files.

backup) print "Please enter the directory to copy your files to:"
read dir
if [ -f $dir ]; then
mkdir $dir
cp /myhomefolder/* $dir
else
cp /myhomefolder/* $dir
fi;;

This is the code I'm using to take the users chosen directory, see if it exists within the current directory and make it if not then copy the files but it's just not working! It just says that the directory entered is not a directory. I'm a bit of a newbie when it comes to this and I've been trawling through websites and books but I just can't get it to work!

Any thoughts? Thankx
  #2 (permalink)  
Old 12-17-2003
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
try useing a -d in your if statment.

-d= is a directory

you might even want to do a -w (you have write premissions)

in ksh -f = is a regular file (ie not a directory or other special type of file.

i added a -p in your mkdir so it makes any directorys that would be missing in the $dir variable instead of just the last directory.

so something liek this:


Code:
if [ -d $dir ]; then
cp /myhomefolder/* $dir
else
mkdir -p $dir 
cp /myhomefolder/* $dir
fi;;

  #3 (permalink)  
Old 12-17-2003
fpmurphy's Avatar
fpmurphy fpmurphy is online now Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,935
Humm, there are a number of problems with this
shell script.

"if [ -f $dir ]" tests that $dir exists and is a regular
file. Use "-d" to test that $dir exists and is a
directory. See the test or [ man page. i.e.
"man test" or "man [".

You also should use the -p option to mkdir to
to create intermediate directories as necessary.
i.e. "mkdir -p $dir".

Another test that should probablly be added is
to ensure that the user actually entered a directory
and did not just hit return. test -n or test -z
will help here.

You also should test that user has permission to
write in the target directory and warm them if they
do not have write permission.

- Finnbarr
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 01:41 AM.


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