How to loop with case esac in shell scripting?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















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









share|improve this question


















  • 3





    The ;; at the end of the case mark the end. Thus, your following break or continue commands are ignored. *) echo "you have entered a valid path" ; break ;; is more like what you need.

    – waltinator
    15 hours ago


















1















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









share|improve this question


















  • 3





    The ;; at the end of the case mark the end. Thus, your following break or continue commands are ignored. *) echo "you have entered a valid path" ; break ;; is more like what you need.

    – waltinator
    15 hours ago














1












1








1








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









share|improve this question














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






command-line bash scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 15 hours ago









JennyJenny

917




917








  • 3





    The ;; at the end of the case mark the end. Thus, your following break or continue commands are ignored. *) echo "you have entered a valid path" ; break ;; is more like what you need.

    – waltinator
    15 hours ago














  • 3





    The ;; at the end of the case mark the end. Thus, your following break or continue commands are ignored. *) echo "you have entered a valid path" ; break ;; is more like what you need.

    – waltinator
    15 hours ago








3




3





The ;; at the end of the case mark the end. Thus, your following break or continue commands are ignored. *) echo "you have entered a valid path" ; break ;; is more like what you need.

– waltinator
15 hours ago





The ;; at the end of the case mark the end. Thus, your following break or continue commands are ignored. *) echo "you have entered a valid path" ; break ;; is more like what you need.

– waltinator
15 hours ago










1 Answer
1






active

oldest

votes


















1














You should really check your indentation. The final done and fi statements are in the wrong order although your indentation suggests otherwise. Another issue is the case statement. The basic syntax is



case $SOMETHING in
value1)
statement1;
statement2;
;;
value2)
statement3;
statement4;
;;
esac


That is: the final ;; must actually be the last statement for each case and indicates its end. If you want to continue in some case, then you need to put that continue statement before any ;;, like so:



#!/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
fi
done
}

function main() {
project1_install_dir_path
}

main;





share|improve this answer



















  • 2





    Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

    – Sergiy Kolodyazhnyy
    14 hours ago











  • @SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

    – PerlDuck
    14 hours ago






  • 1





    Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

    – muclux
    13 hours ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131468%2fhow-to-loop-with-case-esac-in-shell-scripting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You should really check your indentation. The final done and fi statements are in the wrong order although your indentation suggests otherwise. Another issue is the case statement. The basic syntax is



case $SOMETHING in
value1)
statement1;
statement2;
;;
value2)
statement3;
statement4;
;;
esac


That is: the final ;; must actually be the last statement for each case and indicates its end. If you want to continue in some case, then you need to put that continue statement before any ;;, like so:



#!/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
fi
done
}

function main() {
project1_install_dir_path
}

main;





share|improve this answer



















  • 2





    Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

    – Sergiy Kolodyazhnyy
    14 hours ago











  • @SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

    – PerlDuck
    14 hours ago






  • 1





    Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

    – muclux
    13 hours ago
















1














You should really check your indentation. The final done and fi statements are in the wrong order although your indentation suggests otherwise. Another issue is the case statement. The basic syntax is



case $SOMETHING in
value1)
statement1;
statement2;
;;
value2)
statement3;
statement4;
;;
esac


That is: the final ;; must actually be the last statement for each case and indicates its end. If you want to continue in some case, then you need to put that continue statement before any ;;, like so:



#!/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
fi
done
}

function main() {
project1_install_dir_path
}

main;





share|improve this answer



















  • 2





    Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

    – Sergiy Kolodyazhnyy
    14 hours ago











  • @SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

    – PerlDuck
    14 hours ago






  • 1





    Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

    – muclux
    13 hours ago














1












1








1







You should really check your indentation. The final done and fi statements are in the wrong order although your indentation suggests otherwise. Another issue is the case statement. The basic syntax is



case $SOMETHING in
value1)
statement1;
statement2;
;;
value2)
statement3;
statement4;
;;
esac


That is: the final ;; must actually be the last statement for each case and indicates its end. If you want to continue in some case, then you need to put that continue statement before any ;;, like so:



#!/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
fi
done
}

function main() {
project1_install_dir_path
}

main;





share|improve this answer













You should really check your indentation. The final done and fi statements are in the wrong order although your indentation suggests otherwise. Another issue is the case statement. The basic syntax is



case $SOMETHING in
value1)
statement1;
statement2;
;;
value2)
statement3;
statement4;
;;
esac


That is: the final ;; must actually be the last statement for each case and indicates its end. If you want to continue in some case, then you need to put that continue statement before any ;;, like so:



#!/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
fi
done
}

function main() {
project1_install_dir_path
}

main;






share|improve this answer












share|improve this answer



share|improve this answer










answered 14 hours ago









PerlDuckPerlDuck

7,98611636




7,98611636








  • 2





    Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

    – Sergiy Kolodyazhnyy
    14 hours ago











  • @SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

    – PerlDuck
    14 hours ago






  • 1





    Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

    – muclux
    13 hours ago














  • 2





    Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

    – Sergiy Kolodyazhnyy
    14 hours ago











  • @SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

    – PerlDuck
    14 hours ago






  • 1





    Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

    – muclux
    13 hours ago








2




2





Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

– Sergiy Kolodyazhnyy
14 hours ago





Just a note, trailing ; is not necessary if you're using newline to separate commands. If two commands are on the same line, then you do want to use semicolon, as in echo foo ; echo bar

– Sergiy Kolodyazhnyy
14 hours ago













@SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

– PerlDuck
14 hours ago





@SergiyKolodyazhnyy Exactly. It's just a habit of mine because some other languages (Perl, for example) need it. Python, as another example, does not need it and in fact warns you about deprecated or old-fashioned style if you put ; after a statement. Please feel invited to remove the unnecessary ; from this post.

– PerlDuck
14 hours ago




1




1





Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

– muclux
13 hours ago





Three general remarks: You are not really using boolian_2, your are asking textually for a confirmation which is not asked for, and finally there are other strings defining a path in my home that would pass your tests (like ~/mydir, or /*/myusername, ...).

– muclux
13 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1131468%2fhow-to-loop-with-case-esac-in-shell-scripting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

GameSpot

connect to host localhost port 22: Connection refused

Getting a Wifi WPA2 wifi connection