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 remote file exists hcclnoodles Shell Programming and Scripting 2 08-27-2008 05:53 PM
How to check if a direcorty exists? SunnyK Shell Programming and Scripting 2 11-08-2007 10:47 AM
How can I check if directory exists in a makefile zivsegal UNIX for Dummies Questions & Answers 2 09-10-2007 03:12 AM
how to check if masked directory exists? philplasma Shell Programming and Scripting 4 05-30-2007 08:46 PM
perl ftp check file exists methos Shell Programming and Scripting 2 06-18-2003 08:21 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 06-13-2005
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
check if directory exists

Hi,

I need to prompt for a response from a user to enter a path
Code:
read dest_dir?"Please Enter Directory :"
How do I do this until a valid directory is entered by the user. I can use
Code:
[ -d dest_dir ]
to check the existence of the directory. However when I try the following I cannot get it to work.
Code:
while [ -z $dest_dir ]
do
    read dest_dir?"Please Enter Directory :"
    if [ -d $dest_dir ];
       then
             echo "Invalid directory entered. Please try again"
    fi
done
This is what I am getting as an output when I execute the above piece of code.

test.sh: test: 0403-004 Specify a parameter with this command

What am i doing wrong. Please advise. Thanks.
  #2 (permalink)  
Old 06-13-2005
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
quote the $dest_dir in your test.

Code:
[ -z "$dest_dir" ]
  #3 (permalink)  
Old 06-13-2005
jerardfjay jerardfjay is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 146
Quote:
Originally Posted by reborg
quote the $dest_dir in your test.

Code:
[ -z "$dest_dir" ]
Thanks reborg. Now I have the following
Code:
while [ -z "$dest_dir" ]
do
    read dest_dir?"Please Enter Directory :"
    if [ -d $dest_dir ];
       then
             echo "Invalid directory entered. Please try again"
    fi
done
When executed and if the response is just a "CR". I get the following
test.sh
Please Enter Directory :
test.sh[4]: test: 0403-004 Specify a parameter with this command.
Please Enter Directory :

To overcome the same I changed line 4 to also include quotes. Also I included the negation on this check to make it work correctly as follows
Code:
while [ ! -d "$dest_dir" ]
do
    read dest_dir?"Please Enter Directory :"
    if [ ! -d "$dest_dir" ];
       then
             echo "Invalid directory entered. Please try again"
    fi
done
.

Thanks.

Last edited by jerardfjay; 06-13-2005 at 02:33 PM.. Reason: code change
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 07:46 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