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;
}
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
add a comment |
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
3
The;;
at the end of the case mark the end. Thus, your followingbreak
orcontinue
commands are ignored.*) echo "you have entered a valid path" ; break ;;
is more like what you need.
– waltinator
15 hours ago
add a comment |
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
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
command-line bash scripts
asked 15 hours ago
JennyJenny
917
917
3
The;;
at the end of the case mark the end. Thus, your followingbreak
orcontinue
commands are ignored.*) echo "you have entered a valid path" ; break ;;
is more like what you need.
– waltinator
15 hours ago
add a comment |
3
The;;
at the end of the case mark the end. Thus, your followingbreak
orcontinue
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
add a comment |
1 Answer
1
active
oldest
votes
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;
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 inecho 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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 inecho 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
add a comment |
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;
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 inecho 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
add a comment |
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;
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;
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 inecho 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
add a comment |
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 inecho 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
3
The
;;
at the end of the case mark the end. Thus, your followingbreak
orcontinue
commands are ignored.*) echo "you have entered a valid path" ; break ;;
is more like what you need.– waltinator
15 hours ago