I'd like to write a script where if a user enters a path which contains his/her $HOME
directory, it would raise an error and the script will run until user enters a valid path in which the loop will break.
Apparently it gives a syntax error if I have continue
or break
commands. What do I do wrong here? Thanks, Jen.
#!/bin/bash
function project1_install_dir_path() {
boolian_2=true;
while true; do
if [ "$boolian_2" = true ] ; then
read -p "Enter FULL folder path where you want to install colsim1:" fullpath
echo "you have enterd "$fullpath". Please press 'y' to confirm and 'n' to enter again"
case "$fullpath" in
"$HOME"*) echo "Error: The path cannot be in your HOME" ;; continue
*/home*) echo "Error: The path cannot contain 'home' in the path" ;; continue
*) echo "you have entered a valid path" ;; break
esac
done
fi
}
function main() {
project1_install_dir_path
}
Terminal Output
jen@ss23:/bash_file.sh
-bash: /project/bash_file.sh: line 62: syntax error near unexpected token `newline'
-bash: /project/bash_file.sh: line 62: ` "$HOME"*) echo "Error: The path cannot be in your HOME" ;; continue
You should really check your indentation. The final
done
andfi
statements are in the wrong order although your indentation suggests otherwise. Another issue is thecase
statement. The basic syntax isThat is: the final
;;
must actually be the last statement for each case and indicates its end. If you want tocontinue
in some case, then you need to put thatcontinue
statement before any;;
, like so: