Is it possible for a shell script to prompt a user of where to create a directory?
echo -n "Enter folder path location::::"
Is it possible for a shell script to prompt a user of where to create a directory?
echo -n "Enter folder path location::::"
echo -n 'Enter folder path location: '
read folder_path
echo "You entered $folder_path"
echo -n "Enter folder path location::::"
read folderName
echo "$folderName is the directory name inserted"
You can add many controls. I think it's better that you start reading some bash manual and books. Something like Advanced BASH Scripting Guide.
You can use the prompt feature of the read, like:
err() {
echo "Error: $@" >&2 ; return 1
}
while :
do
read -r -p "Enter directory name > " newdir
#check already exists
[[ ! -e "$newdir" ]] || err "$newdir exists" || continue
#if want to find real parent directory
#real="$( cd "$(dirname "$newdir" )" && /bin/pwd -P )" || continue
#some other safety tests here if need
#create a directory, if success - break the cycle
mkdir "$newdir" && break
#otherwire repeat the question...
done
#ok
Try this:
while read -ep "Enter folder path location: " file_dir; do
if [ -d "${file_dir}" ]; then
echo "${file_dir} already exists - please enter a valid directory path."
else
mkdir -p "${file_dir}"
break
fi
done