I'm trying to prompt user repeatedly enter a directory and then the script will then check if the directory exists, if not keep prompting till the user enter a valid directory.
my code:
#!/bin/bash
while :
do
echo "Enter a directory:"
read directory
if [ -f $directory ]
then
cd $directory
break
else
echo "Directory does not exists, please try again."
fi
done
The problem now is even when I enter the valid directory e.g /home/username it's still looping instead of changing that directory, any idea what went wrong?
-f
is a test for regular files; it's-d
for directories – steeldriver Feb 11 '20 at 13:20