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 08:49 AM
Can usfdump be used for backing up 1 directory? FredSmith SUN Solaris 5 01-12-2007 09:31 AM
backing up the vtoc BG_JrAdmin UNIX for Dummies Questions & Answers 1 12-22-2005 04:33 PM
Best practises for backing up d11wtq UNIX for Dummies Questions & Answers 2 07-06-2005 05:11 PM
backing up merlin UNIX for Dummies Questions & Answers 4 09-09-2001 01:35 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-17-2003
Registered User
 

Join Date: Dec 2003
Posts: 4
Stumble this Post!
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
Forum Sponsor
  #2 (permalink)  
Old 12-17-2003
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
Stumble this Post!
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
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 951
Stumble this Post!
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
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0