Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I'm trying to prompt the user to supply a path name to a file. Once I read the pathname to a variable in the script, I need to check whether the directory exist or not. If not , then I need to ask the user for reentering the path.
Can anyone show me how to accomplish this ?
Thanks in advance,
abby

Do a man on 'test'. You'll see that there are numerous flags for testing things like the existence of a directory, a file, a non-zero size file, etc.
This code will loop until a valid path is entered. I'd suggest modifying it to contain a counter, so that it'll exit after 3 failures or somesuch.
validPath=0while [ $validPath -eq 0 ]
doecho
echo Please enter the path
read myPath
if [ -d $myPath ]
thenvalidPath=1
elseecho Sorry, that path is invalid
fidone
echo
echo $myPath is valid
~

Just make it a function
Best to make it a function so you can call on it at anytime
#!/bin/ksh
function chkdirectory {
read getdirectory"?Give me Directory> "
if [[ -d $getdirectory ]];then
echo Directory is there!
else
echo Bad Directory
chkdirectory
fi
}
chkdirectory

![]() |
script -entering password...
|
Solaris talking to Intern...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |