![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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;; |
|
|||
|
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 |
| Thread Tools | |
| Display Modes | |
|
|