I need to prompt for a response from a user to enter a path
read dest_dir?"Please Enter Directory :"
How do I do this until a valid directory is entered by the user. I can use
[ -d dest_dir ]
to check the existence of the directory. However when I try the following I cannot get it to work.
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
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
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