Write a regular expression which catches user $HOME in shellscripting
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'd like to write a regex
in shellscript
which would compare the user input path to user$HOME
. Then it would raise an error in case the user input path contains the user $HOME
. The home folder normally starts with /ort/home
but /ort
part is not a must. When I run it always gives a valid path
although I clearly enter an invalid path. What am I doing wrong? Thanks. Please refer following link[1] if necessary to see how regex
are used in shellscript
My attempt is as follows
#!/bin/bash
function user_input () {
read -p "Type the path for project1:" user_path
string1="${HOME}"
echo "$string1"
if [ "$user_path" = "$string1 | /ort/home/+.* | .*+/home/+.*" ]; then
echo "Not a valid path, contains $HOME directory"
else
echo "valid path"
fi
}
function main() {
user_input
}
main
Terminal output
Please note first entry is a valid path and other two are invalid paths.
jenny@server32:~$ ./test.sh
Type the path for project1:/scratch/random
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home/j/jen
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home
/ort/home/j/jen
valid path
[1]https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash
command-line bash scripts regex
add a comment |
I'd like to write a regex
in shellscript
which would compare the user input path to user$HOME
. Then it would raise an error in case the user input path contains the user $HOME
. The home folder normally starts with /ort/home
but /ort
part is not a must. When I run it always gives a valid path
although I clearly enter an invalid path. What am I doing wrong? Thanks. Please refer following link[1] if necessary to see how regex
are used in shellscript
My attempt is as follows
#!/bin/bash
function user_input () {
read -p "Type the path for project1:" user_path
string1="${HOME}"
echo "$string1"
if [ "$user_path" = "$string1 | /ort/home/+.* | .*+/home/+.*" ]; then
echo "Not a valid path, contains $HOME directory"
else
echo "valid path"
fi
}
function main() {
user_input
}
main
Terminal output
Please note first entry is a valid path and other two are invalid paths.
jenny@server32:~$ ./test.sh
Type the path for project1:/scratch/random
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home/j/jen
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home
/ort/home/j/jen
valid path
[1]https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash
command-line bash scripts regex
2
Regular expression comparison and string (glob pattern) comparison are different things, with different operators in bash (=~
versus==
or=
); additionally,=~
is only supported within extended test brackets[[ ... ]]
afaik
– steeldriver
Apr 1 at 22:32
add a comment |
I'd like to write a regex
in shellscript
which would compare the user input path to user$HOME
. Then it would raise an error in case the user input path contains the user $HOME
. The home folder normally starts with /ort/home
but /ort
part is not a must. When I run it always gives a valid path
although I clearly enter an invalid path. What am I doing wrong? Thanks. Please refer following link[1] if necessary to see how regex
are used in shellscript
My attempt is as follows
#!/bin/bash
function user_input () {
read -p "Type the path for project1:" user_path
string1="${HOME}"
echo "$string1"
if [ "$user_path" = "$string1 | /ort/home/+.* | .*+/home/+.*" ]; then
echo "Not a valid path, contains $HOME directory"
else
echo "valid path"
fi
}
function main() {
user_input
}
main
Terminal output
Please note first entry is a valid path and other two are invalid paths.
jenny@server32:~$ ./test.sh
Type the path for project1:/scratch/random
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home/j/jen
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home
/ort/home/j/jen
valid path
[1]https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash
command-line bash scripts regex
I'd like to write a regex
in shellscript
which would compare the user input path to user$HOME
. Then it would raise an error in case the user input path contains the user $HOME
. The home folder normally starts with /ort/home
but /ort
part is not a must. When I run it always gives a valid path
although I clearly enter an invalid path. What am I doing wrong? Thanks. Please refer following link[1] if necessary to see how regex
are used in shellscript
My attempt is as follows
#!/bin/bash
function user_input () {
read -p "Type the path for project1:" user_path
string1="${HOME}"
echo "$string1"
if [ "$user_path" = "$string1 | /ort/home/+.* | .*+/home/+.*" ]; then
echo "Not a valid path, contains $HOME directory"
else
echo "valid path"
fi
}
function main() {
user_input
}
main
Terminal output
Please note first entry is a valid path and other two are invalid paths.
jenny@server32:~$ ./test.sh
Type the path for project1:/scratch/random
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home/j/jen
/ort/home/j/jen
valid path
jenny@server32:~$ ./test.sh
Type the path for project1:/ort/home
/ort/home/j/jen
valid path
[1]https://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash
command-line bash scripts regex
command-line bash scripts regex
asked Apr 1 at 22:21
JennyJenny
1018
1018
2
Regular expression comparison and string (glob pattern) comparison are different things, with different operators in bash (=~
versus==
or=
); additionally,=~
is only supported within extended test brackets[[ ... ]]
afaik
– steeldriver
Apr 1 at 22:32
add a comment |
2
Regular expression comparison and string (glob pattern) comparison are different things, with different operators in bash (=~
versus==
or=
); additionally,=~
is only supported within extended test brackets[[ ... ]]
afaik
– steeldriver
Apr 1 at 22:32
2
2
Regular expression comparison and string (glob pattern) comparison are different things, with different operators in bash (
=~
versus ==
or =
); additionally, =~
is only supported within extended test brackets [[ ... ]]
afaik– steeldriver
Apr 1 at 22:32
Regular expression comparison and string (glob pattern) comparison are different things, with different operators in bash (
=~
versus ==
or =
); additionally, =~
is only supported within extended test brackets [[ ... ]]
afaik– steeldriver
Apr 1 at 22:32
add a comment |
1 Answer
1
active
oldest
votes
The case
command uses glob wildcards which are pretty easy to work with:
case "$user_path" in
"$HOME"*) echo "Error: path cannot be in your HOME" ;;
*/home/*) echo "Error: cannot have 'home' in the path" ;;
*) echo "Thank you, valid path" ;;
esac
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%2f1130471%2fwrite-a-regular-expression-which-catches-user-home-in-shellscripting%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
The case
command uses glob wildcards which are pretty easy to work with:
case "$user_path" in
"$HOME"*) echo "Error: path cannot be in your HOME" ;;
*/home/*) echo "Error: cannot have 'home' in the path" ;;
*) echo "Thank you, valid path" ;;
esac
add a comment |
The case
command uses glob wildcards which are pretty easy to work with:
case "$user_path" in
"$HOME"*) echo "Error: path cannot be in your HOME" ;;
*/home/*) echo "Error: cannot have 'home' in the path" ;;
*) echo "Thank you, valid path" ;;
esac
add a comment |
The case
command uses glob wildcards which are pretty easy to work with:
case "$user_path" in
"$HOME"*) echo "Error: path cannot be in your HOME" ;;
*/home/*) echo "Error: cannot have 'home' in the path" ;;
*) echo "Thank you, valid path" ;;
esac
The case
command uses glob wildcards which are pretty easy to work with:
case "$user_path" in
"$HOME"*) echo "Error: path cannot be in your HOME" ;;
*/home/*) echo "Error: cannot have 'home' in the path" ;;
*) echo "Thank you, valid path" ;;
esac
answered Apr 1 at 22:40
glenn jackmanglenn jackman
12.8k2545
12.8k2545
add a comment |
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%2f1130471%2fwrite-a-regular-expression-which-catches-user-home-in-shellscripting%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
2
Regular expression comparison and string (glob pattern) comparison are different things, with different operators in bash (
=~
versus==
or=
); additionally,=~
is only supported within extended test brackets[[ ... ]]
afaik– steeldriver
Apr 1 at 22:32