Using the not equal operator for string comparison The 2019 Stack Overflow Developer Survey Results Are InMultiple string comparison in a single if statement shell script using OR gateChecking first argument of a script if it is -e or -dBash not equal string comparisonWhy does this comparison return not equal?How to print the values of variables with incremented numbers using a loop in shell script?Conditional execution block with || and parentheses problemexit code of the command **before** last?bash or Ksh quotes and command execRemove all duplicate word from string using shell scriptHow to output a shell command into a variable?while loop to check for user input not in for loopProtection of shell command with string variable
What is the motivation for a law requiring 2 parties to consent for recording a conversation
For what reasons would an animal species NOT cross a *horizontal* land bridge?
Correct punctuation for showing a character's confusion
Keeping a retro style to sci-fi spaceships?
What is preventing me from simply constructing a hash that's lower than the current target?
The phrase "to the numbers born"?
Loose spokes after only a few rides
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
Getting crown tickets for Statue of Liberty
How do you keep chess fun when your opponent constantly beats you?
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
Short story: man watches girlfriend's spaceship entering a 'black hole' (?) forever
Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?
Accepted by European university, rejected by all American ones I applied to? Possible reasons?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Pokemon Turn Based battle (Python)
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
Why isn't the circumferential light around the M87 black hole's event horizon symmetric?
How do I free up internal storage if I don't have any apps downloaded?
ELI5: Why they say that Israel would have been the fourth country to land a spacecraft on the Moon and why they call it low cost?
How to type this arrow in math mode?
What is this sharp, curved notch on my knife for?
How to support a colleague who finds meetings extremely tiring?
Using the not equal operator for string comparison
The 2019 Stack Overflow Developer Survey Results Are InMultiple string comparison in a single if statement shell script using OR gateChecking first argument of a script if it is -e or -dBash not equal string comparisonWhy does this comparison return not equal?How to print the values of variables with incremented numbers using a loop in shell script?Conditional execution block with || and parentheses problemexit code of the command **before** last?bash or Ksh quotes and command execRemove all duplicate word from string using shell scriptHow to output a shell command into a variable?while loop to check for user input not in for loopProtection of shell command with string variable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I tried to check if the PHONE_TYPE
variable contains one of three valid values.
if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
[ "$PHONE_TYPE" != "CISCO" ]
then
echo "Phone type must be nortel,cisco or nec"
exit
fi
The above code did not work for me, so I tried this instead:
if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
[ "$PHONE_TYPE" == "CISCO" ]
then
: # do nothing
else
echo "Phone type must be nortel,cisco or nec"
exit
fi
Are there cleaner ways for this type of task?
shell-script shell
add a comment |
I tried to check if the PHONE_TYPE
variable contains one of three valid values.
if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
[ "$PHONE_TYPE" != "CISCO" ]
then
echo "Phone type must be nortel,cisco or nec"
exit
fi
The above code did not work for me, so I tried this instead:
if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
[ "$PHONE_TYPE" == "CISCO" ]
then
: # do nothing
else
echo "Phone type must be nortel,cisco or nec"
exit
fi
Are there cleaner ways for this type of task?
shell-script shell
add a comment |
I tried to check if the PHONE_TYPE
variable contains one of three valid values.
if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
[ "$PHONE_TYPE" != "CISCO" ]
then
echo "Phone type must be nortel,cisco or nec"
exit
fi
The above code did not work for me, so I tried this instead:
if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
[ "$PHONE_TYPE" == "CISCO" ]
then
: # do nothing
else
echo "Phone type must be nortel,cisco or nec"
exit
fi
Are there cleaner ways for this type of task?
shell-script shell
I tried to check if the PHONE_TYPE
variable contains one of three valid values.
if [ "$PHONE_TYPE" != "NORTEL" ] || [ "$PHONE_TYPE" != "NEC" ] ||
[ "$PHONE_TYPE" != "CISCO" ]
then
echo "Phone type must be nortel,cisco or nec"
exit
fi
The above code did not work for me, so I tried this instead:
if [ "$PHONE_TYPE" == "NORTEL" ] || [ "$PHONE_TYPE" == "NEC" ] ||
[ "$PHONE_TYPE" == "CISCO" ]
then
: # do nothing
else
echo "Phone type must be nortel,cisco or nec"
exit
fi
Are there cleaner ways for this type of task?
shell-script shell
shell-script shell
edited Feb 12 at 20:06
ilkkachu
63.4k10104181
63.4k10104181
asked Mar 14 '13 at 9:50
munishmunish
2,687165282
2,687165282
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
I guess you're looking for:
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
[ "$PHONE_TYPE" != "CISCO" ]
The rules for these equivalents are called De Morgan's laws and in your case meant:
not(A || B || C) => not(A) && not(B) && not (C)
Note the change in the boolean operator or and and.
Whereas you tried to do:
not(A || B || C) => not(A) || not(B) || not(C)
Which obviously doesn't work.
add a comment |
A much shorter way would be:
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
echo "Phone type must be nortel, cisco or nec."
fi
I think that should beif [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
add a comment |
You should use ANDs, not ORs.
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
then
or
if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
then
add a comment |
Good answers, and an invaluable lesson ;) Only want to supplement with a note.
What type of test one choose to use is highly dependent on code, structure, surroundings etc.
An alternative could be to use a switch or case
statement as in:
case "$PHONE_TYPE" in
"NORTEL"|"NEC"|"CISCO")
echo "OK"
;;
*)
echo "Phone type must be nortel,cisco or nec"
;;
esac
As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type
instead of $PHONE_TYPE
.
Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo"
and you're in a world of hurt.
It will also make it easier to spot which is what.
Not a have to but a would strongly consider.
It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:
valid_phone_type()
case "$1" in
"NORTEL"
if ! valid_phone_type "$phone_type"; then
echo "Bye."
exit 1
fi
add a comment |
Use [[ instead
if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] ||
[[ "$PHONE_TYPE" != "CISCO" ]]
then
echo "Phone type must be nortel,cisco or nec"
exit 1
fi
This is, of course, wrong.[[
vs[
doesn't help with the logic being off.
– ilkkachu
Feb 12 at 20:10
add a comment |
To correct an above answer (as I can't comment yet):
PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
echo "Phone type accepted."
else
echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi
Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.
I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)
The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.
add a comment |
Just a variation proposal based on @0x80 solution:
# define phone brand list
phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place
# test if user given phone is contained in the list
if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
echo "found it !"
fi
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f67898%2fusing-the-not-equal-operator-for-string-comparison%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
I guess you're looking for:
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
[ "$PHONE_TYPE" != "CISCO" ]
The rules for these equivalents are called De Morgan's laws and in your case meant:
not(A || B || C) => not(A) && not(B) && not (C)
Note the change in the boolean operator or and and.
Whereas you tried to do:
not(A || B || C) => not(A) || not(B) || not(C)
Which obviously doesn't work.
add a comment |
I guess you're looking for:
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
[ "$PHONE_TYPE" != "CISCO" ]
The rules for these equivalents are called De Morgan's laws and in your case meant:
not(A || B || C) => not(A) && not(B) && not (C)
Note the change in the boolean operator or and and.
Whereas you tried to do:
not(A || B || C) => not(A) || not(B) || not(C)
Which obviously doesn't work.
add a comment |
I guess you're looking for:
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
[ "$PHONE_TYPE" != "CISCO" ]
The rules for these equivalents are called De Morgan's laws and in your case meant:
not(A || B || C) => not(A) && not(B) && not (C)
Note the change in the boolean operator or and and.
Whereas you tried to do:
not(A || B || C) => not(A) || not(B) || not(C)
Which obviously doesn't work.
I guess you're looking for:
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] &&
[ "$PHONE_TYPE" != "CISCO" ]
The rules for these equivalents are called De Morgan's laws and in your case meant:
not(A || B || C) => not(A) && not(B) && not (C)
Note the change in the boolean operator or and and.
Whereas you tried to do:
not(A || B || C) => not(A) || not(B) || not(C)
Which obviously doesn't work.
edited Feb 12 at 20:07
ilkkachu
63.4k10104181
63.4k10104181
answered Mar 14 '13 at 9:57
Nils WernerNils Werner
1,71521113
1,71521113
add a comment |
add a comment |
A much shorter way would be:
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
echo "Phone type must be nortel, cisco or nec."
fi
I think that should beif [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
add a comment |
A much shorter way would be:
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
echo "Phone type must be nortel, cisco or nec."
fi
I think that should beif [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
add a comment |
A much shorter way would be:
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
echo "Phone type must be nortel, cisco or nec."
fi
A much shorter way would be:
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
echo "Phone type must be nortel, cisco or nec."
fi
edited Mar 14 '13 at 17:14
answered Mar 14 '13 at 15:26
0x800x80
43327
43327
I think that should beif [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
add a comment |
I think that should beif [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
I think that should be
if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
I think that should be
if [[ ! $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then
– Milan Simek
6 hours ago
add a comment |
You should use ANDs, not ORs.
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
then
or
if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
then
add a comment |
You should use ANDs, not ORs.
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
then
or
if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
then
add a comment |
You should use ANDs, not ORs.
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
then
or
if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
then
You should use ANDs, not ORs.
if [ "$PHONE_TYPE" != "NORTEL" ] && [ "$PHONE_TYPE" != "NEC" ] && [ "$PHONE_TYPE" != "CISCO" ]
then
or
if [ "$PHONE_TYPE" != "NORTEL" -a "$PHONE_TYPE" != "NEC" -a "$PHONE_TYPE" != "CISCO" ]
then
answered Mar 14 '13 at 9:57
jlliagrejlliagre
47.9k786138
47.9k786138
add a comment |
add a comment |
Good answers, and an invaluable lesson ;) Only want to supplement with a note.
What type of test one choose to use is highly dependent on code, structure, surroundings etc.
An alternative could be to use a switch or case
statement as in:
case "$PHONE_TYPE" in
"NORTEL"|"NEC"|"CISCO")
echo "OK"
;;
*)
echo "Phone type must be nortel,cisco or nec"
;;
esac
As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type
instead of $PHONE_TYPE
.
Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo"
and you're in a world of hurt.
It will also make it easier to spot which is what.
Not a have to but a would strongly consider.
It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:
valid_phone_type()
case "$1" in
"NORTEL"
if ! valid_phone_type "$phone_type"; then
echo "Bye."
exit 1
fi
add a comment |
Good answers, and an invaluable lesson ;) Only want to supplement with a note.
What type of test one choose to use is highly dependent on code, structure, surroundings etc.
An alternative could be to use a switch or case
statement as in:
case "$PHONE_TYPE" in
"NORTEL"|"NEC"|"CISCO")
echo "OK"
;;
*)
echo "Phone type must be nortel,cisco or nec"
;;
esac
As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type
instead of $PHONE_TYPE
.
Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo"
and you're in a world of hurt.
It will also make it easier to spot which is what.
Not a have to but a would strongly consider.
It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:
valid_phone_type()
case "$1" in
"NORTEL"
if ! valid_phone_type "$phone_type"; then
echo "Bye."
exit 1
fi
add a comment |
Good answers, and an invaluable lesson ;) Only want to supplement with a note.
What type of test one choose to use is highly dependent on code, structure, surroundings etc.
An alternative could be to use a switch or case
statement as in:
case "$PHONE_TYPE" in
"NORTEL"|"NEC"|"CISCO")
echo "OK"
;;
*)
echo "Phone type must be nortel,cisco or nec"
;;
esac
As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type
instead of $PHONE_TYPE
.
Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo"
and you're in a world of hurt.
It will also make it easier to spot which is what.
Not a have to but a would strongly consider.
It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:
valid_phone_type()
case "$1" in
"NORTEL"
if ! valid_phone_type "$phone_type"; then
echo "Bye."
exit 1
fi
Good answers, and an invaluable lesson ;) Only want to supplement with a note.
What type of test one choose to use is highly dependent on code, structure, surroundings etc.
An alternative could be to use a switch or case
statement as in:
case "$PHONE_TYPE" in
"NORTEL"|"NEC"|"CISCO")
echo "OK"
;;
*)
echo "Phone type must be nortel,cisco or nec"
;;
esac
As a second note you should be careful by using upper-case variable names. This is to prevent collision between variables introduced by the system, which almost always is all upper case. Thus $phone_type
instead of $PHONE_TYPE
.
Though that one is safe, if you have as habit using all upper case, one day you might say IFS="boo"
and you're in a world of hurt.
It will also make it easier to spot which is what.
Not a have to but a would strongly consider.
It is also presumably a good candidate for a function. This mostly makes the code easier to read and maintain. E.g.:
valid_phone_type()
case "$1" in
"NORTEL"
if ! valid_phone_type "$phone_type"; then
echo "Bye."
exit 1
fi
edited Mar 14 '13 at 11:35
answered Mar 14 '13 at 11:25
RuniumRunium
18.9k43060
18.9k43060
add a comment |
add a comment |
Use [[ instead
if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] ||
[[ "$PHONE_TYPE" != "CISCO" ]]
then
echo "Phone type must be nortel,cisco or nec"
exit 1
fi
This is, of course, wrong.[[
vs[
doesn't help with the logic being off.
– ilkkachu
Feb 12 at 20:10
add a comment |
Use [[ instead
if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] ||
[[ "$PHONE_TYPE" != "CISCO" ]]
then
echo "Phone type must be nortel,cisco or nec"
exit 1
fi
This is, of course, wrong.[[
vs[
doesn't help with the logic being off.
– ilkkachu
Feb 12 at 20:10
add a comment |
Use [[ instead
if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] ||
[[ "$PHONE_TYPE" != "CISCO" ]]
then
echo "Phone type must be nortel,cisco or nec"
exit 1
fi
Use [[ instead
if [[ "$PHONE_TYPE" != "NORTEL" ]] || [[ "$PHONE_TYPE" != "NEC" ]] ||
[[ "$PHONE_TYPE" != "CISCO" ]]
then
echo "Phone type must be nortel,cisco or nec"
exit 1
fi
edited Jul 8 '15 at 19:26
answered Jul 8 '15 at 19:20
SwapnilSwapnil
273
273
This is, of course, wrong.[[
vs[
doesn't help with the logic being off.
– ilkkachu
Feb 12 at 20:10
add a comment |
This is, of course, wrong.[[
vs[
doesn't help with the logic being off.
– ilkkachu
Feb 12 at 20:10
This is, of course, wrong.
[[
vs [
doesn't help with the logic being off.– ilkkachu
Feb 12 at 20:10
This is, of course, wrong.
[[
vs [
doesn't help with the logic being off.– ilkkachu
Feb 12 at 20:10
add a comment |
To correct an above answer (as I can't comment yet):
PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
echo "Phone type accepted."
else
echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi
Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.
I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)
The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.
add a comment |
To correct an above answer (as I can't comment yet):
PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
echo "Phone type accepted."
else
echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi
Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.
I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)
The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.
add a comment |
To correct an above answer (as I can't comment yet):
PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
echo "Phone type accepted."
else
echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi
Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.
I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)
The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.
To correct an above answer (as I can't comment yet):
PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then
echo "Phone type accepted."
else
echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi
Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.
I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)
The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.
edited Apr 13 '18 at 16:35
answered Apr 13 '18 at 14:49
WillWill
213
213
add a comment |
add a comment |
Just a variation proposal based on @0x80 solution:
# define phone brand list
phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place
# test if user given phone is contained in the list
if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
echo "found it !"
fi
add a comment |
Just a variation proposal based on @0x80 solution:
# define phone brand list
phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place
# test if user given phone is contained in the list
if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
echo "found it !"
fi
add a comment |
Just a variation proposal based on @0x80 solution:
# define phone brand list
phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place
# test if user given phone is contained in the list
if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
echo "found it !"
fi
Just a variation proposal based on @0x80 solution:
# define phone brand list
phoneBrandList=" NORTEL NEC CISCO" ## separator is space with an extra space in first place
# test if user given phone is contained in the list
if [[ $phoneBrandList =~ (^|[[:space:]])"$userPhoneBrand"($|[[:space:]]) ]]; then
echo "found it !"
fi
answered yesterday
tdagettdaget
1014
1014
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f67898%2fusing-the-not-equal-operator-for-string-comparison%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
-shell, shell-script