Multiple String Comparison in Bash

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
ROOTPATH="/path/here"
p1 = "file1"
p2 = "file2"
for file in `find $ROOTPATH type f`; do
if [["$file" =~ $p1]]; then
echo 'got p1'
elif [["$file" =~ $p2]]; then
echo 'got p1'
else
echo 'got nothing'
This fails, and I'm not sure why. Both $p1 and $p2 are strings, and so is file.
Im trying to do a comparison on (2) strings, to see if $p1 or p2 exist in $file on two separate conditions.
What am I doing wrong?
bash string
add a comment |Â
up vote
0
down vote
favorite
ROOTPATH="/path/here"
p1 = "file1"
p2 = "file2"
for file in `find $ROOTPATH type f`; do
if [["$file" =~ $p1]]; then
echo 'got p1'
elif [["$file" =~ $p2]]; then
echo 'got p1'
else
echo 'got nothing'
This fails, and I'm not sure why. Both $p1 and $p2 are strings, and so is file.
Im trying to do a comparison on (2) strings, to see if $p1 or p2 exist in $file on two separate conditions.
What am I doing wrong?
bash string
and a possible copy/paste typo regarding the match for $p2 reporting "got p1"; and the extra surrounding spaces on the p1/p2 assignments
â Jeff Schaller
Oct 16 '17 at 16:06
1
you might use shellcheck.net as a first-pass through the syntax errors, then include any remaining errors in your Question here
â Jeff Schaller
Oct 16 '17 at 16:07
1
See Why is looping over find's output bad practice?
â don_crissti
Oct 16 '17 at 16:21
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
ROOTPATH="/path/here"
p1 = "file1"
p2 = "file2"
for file in `find $ROOTPATH type f`; do
if [["$file" =~ $p1]]; then
echo 'got p1'
elif [["$file" =~ $p2]]; then
echo 'got p1'
else
echo 'got nothing'
This fails, and I'm not sure why. Both $p1 and $p2 are strings, and so is file.
Im trying to do a comparison on (2) strings, to see if $p1 or p2 exist in $file on two separate conditions.
What am I doing wrong?
bash string
ROOTPATH="/path/here"
p1 = "file1"
p2 = "file2"
for file in `find $ROOTPATH type f`; do
if [["$file" =~ $p1]]; then
echo 'got p1'
elif [["$file" =~ $p2]]; then
echo 'got p1'
else
echo 'got nothing'
This fails, and I'm not sure why. Both $p1 and $p2 are strings, and so is file.
Im trying to do a comparison on (2) strings, to see if $p1 or p2 exist in $file on two separate conditions.
What am I doing wrong?
bash string
asked Oct 16 '17 at 15:55
john jones
1053
1053
and a possible copy/paste typo regarding the match for $p2 reporting "got p1"; and the extra surrounding spaces on the p1/p2 assignments
â Jeff Schaller
Oct 16 '17 at 16:06
1
you might use shellcheck.net as a first-pass through the syntax errors, then include any remaining errors in your Question here
â Jeff Schaller
Oct 16 '17 at 16:07
1
See Why is looping over find's output bad practice?
â don_crissti
Oct 16 '17 at 16:21
add a comment |Â
and a possible copy/paste typo regarding the match for $p2 reporting "got p1"; and the extra surrounding spaces on the p1/p2 assignments
â Jeff Schaller
Oct 16 '17 at 16:06
1
you might use shellcheck.net as a first-pass through the syntax errors, then include any remaining errors in your Question here
â Jeff Schaller
Oct 16 '17 at 16:07
1
See Why is looping over find's output bad practice?
â don_crissti
Oct 16 '17 at 16:21
and a possible copy/paste typo regarding the match for $p2 reporting "got p1"; and the extra surrounding spaces on the p1/p2 assignments
â Jeff Schaller
Oct 16 '17 at 16:06
and a possible copy/paste typo regarding the match for $p2 reporting "got p1"; and the extra surrounding spaces on the p1/p2 assignments
â Jeff Schaller
Oct 16 '17 at 16:06
1
1
you might use shellcheck.net as a first-pass through the syntax errors, then include any remaining errors in your Question here
â Jeff Schaller
Oct 16 '17 at 16:07
you might use shellcheck.net as a first-pass through the syntax errors, then include any remaining errors in your Question here
â Jeff Schaller
Oct 16 '17 at 16:07
1
1
See Why is looping over find's output bad practice?
â don_crissti
Oct 16 '17 at 16:21
See Why is looping over find's output bad practice?
â don_crissti
Oct 16 '17 at 16:21
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You are missing required syntax to use find's "search-by-type" operator:
for file in $(find "$ROOTPATH" -type f); do
Your assignation of the p1 and p2 variables are syntactically incorrect:
p1="file1" # Assigns the value 'file1' to the variable p1
p1 = "file1" # Attempts to execute `p1` with arguments '=' and 'file1'
Also, the two echo statements are identical, you might want to alter the second case's echo command, depending upon your use case.
Further, the syntax of your if statements is flawed; a whitespace character or command separator is required both before and after the [[ and ]] tokens.
It also worth to mention about missed spaces[["$file"...
â RomanPerekhrest
Oct 16 '17 at 16:04
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
@johnjones you need spaces as in[[ "$file" .... ]]. Though quoting the variable within[[is not necessary. Also looping throughfindas mentioned by @DopeGhoti is wrong.
â Valentin B
Oct 16 '17 at 17:24
Try something like this:find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note.=~matches regular expression. It matchesfile1.
â Valentin B
Oct 16 '17 at 17:28
add a comment |Â
up vote
0
down vote
Just fixing the syntax
ROOTPATH="/path/here"
p1="file1"
p2="file2"
for file in `find $ROOTPATH -type f`;
do
if [[ $file =~ $p1 ]];
then
echo "got p1"
elif [[ $file =~ $p2 ]];
then
echo "got p2"
else
echo "got nothing"
fi
done
Arrangement
- Removing spaces in variable assignment
-typeinstead oftype- Removed
"in variable$filebecause of[[usage - Added space (
[[ $file&p1 ]]) - Adding
fi&doneterminations
As commented in comments, please be aware of looping with find command.
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You are missing required syntax to use find's "search-by-type" operator:
for file in $(find "$ROOTPATH" -type f); do
Your assignation of the p1 and p2 variables are syntactically incorrect:
p1="file1" # Assigns the value 'file1' to the variable p1
p1 = "file1" # Attempts to execute `p1` with arguments '=' and 'file1'
Also, the two echo statements are identical, you might want to alter the second case's echo command, depending upon your use case.
Further, the syntax of your if statements is flawed; a whitespace character or command separator is required both before and after the [[ and ]] tokens.
It also worth to mention about missed spaces[["$file"...
â RomanPerekhrest
Oct 16 '17 at 16:04
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
@johnjones you need spaces as in[[ "$file" .... ]]. Though quoting the variable within[[is not necessary. Also looping throughfindas mentioned by @DopeGhoti is wrong.
â Valentin B
Oct 16 '17 at 17:24
Try something like this:find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note.=~matches regular expression. It matchesfile1.
â Valentin B
Oct 16 '17 at 17:28
add a comment |Â
up vote
2
down vote
accepted
You are missing required syntax to use find's "search-by-type" operator:
for file in $(find "$ROOTPATH" -type f); do
Your assignation of the p1 and p2 variables are syntactically incorrect:
p1="file1" # Assigns the value 'file1' to the variable p1
p1 = "file1" # Attempts to execute `p1` with arguments '=' and 'file1'
Also, the two echo statements are identical, you might want to alter the second case's echo command, depending upon your use case.
Further, the syntax of your if statements is flawed; a whitespace character or command separator is required both before and after the [[ and ]] tokens.
It also worth to mention about missed spaces[["$file"...
â RomanPerekhrest
Oct 16 '17 at 16:04
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
@johnjones you need spaces as in[[ "$file" .... ]]. Though quoting the variable within[[is not necessary. Also looping throughfindas mentioned by @DopeGhoti is wrong.
â Valentin B
Oct 16 '17 at 17:24
Try something like this:find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note.=~matches regular expression. It matchesfile1.
â Valentin B
Oct 16 '17 at 17:28
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You are missing required syntax to use find's "search-by-type" operator:
for file in $(find "$ROOTPATH" -type f); do
Your assignation of the p1 and p2 variables are syntactically incorrect:
p1="file1" # Assigns the value 'file1' to the variable p1
p1 = "file1" # Attempts to execute `p1` with arguments '=' and 'file1'
Also, the two echo statements are identical, you might want to alter the second case's echo command, depending upon your use case.
Further, the syntax of your if statements is flawed; a whitespace character or command separator is required both before and after the [[ and ]] tokens.
You are missing required syntax to use find's "search-by-type" operator:
for file in $(find "$ROOTPATH" -type f); do
Your assignation of the p1 and p2 variables are syntactically incorrect:
p1="file1" # Assigns the value 'file1' to the variable p1
p1 = "file1" # Attempts to execute `p1` with arguments '=' and 'file1'
Also, the two echo statements are identical, you might want to alter the second case's echo command, depending upon your use case.
Further, the syntax of your if statements is flawed; a whitespace character or command separator is required both before and after the [[ and ]] tokens.
edited Oct 16 '17 at 16:58
answered Oct 16 '17 at 16:03
DopeGhoti
40.7k54979
40.7k54979
It also worth to mention about missed spaces[["$file"...
â RomanPerekhrest
Oct 16 '17 at 16:04
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
@johnjones you need spaces as in[[ "$file" .... ]]. Though quoting the variable within[[is not necessary. Also looping throughfindas mentioned by @DopeGhoti is wrong.
â Valentin B
Oct 16 '17 at 17:24
Try something like this:find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note.=~matches regular expression. It matchesfile1.
â Valentin B
Oct 16 '17 at 17:28
add a comment |Â
It also worth to mention about missed spaces[["$file"...
â RomanPerekhrest
Oct 16 '17 at 16:04
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
@johnjones you need spaces as in[[ "$file" .... ]]. Though quoting the variable within[[is not necessary. Also looping throughfindas mentioned by @DopeGhoti is wrong.
â Valentin B
Oct 16 '17 at 17:24
Try something like this:find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note.=~matches regular expression. It matchesfile1.
â Valentin B
Oct 16 '17 at 17:28
It also worth to mention about missed spaces
[["$file" ...â RomanPerekhrest
Oct 16 '17 at 16:04
It also worth to mention about missed spaces
[["$file" ...â RomanPerekhrest
Oct 16 '17 at 16:04
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
Can you elaborate where im missing spaces please
â john jones
Oct 16 '17 at 16:07
@johnjones you need spaces as in
[[ "$file" .... ]]. Though quoting the variable within [[ is not necessary. Also looping through find as mentioned by @DopeGhoti is wrong.â Valentin B
Oct 16 '17 at 17:24
@johnjones you need spaces as in
[[ "$file" .... ]]. Though quoting the variable within [[ is not necessary. Also looping through find as mentioned by @DopeGhoti is wrong.â Valentin B
Oct 16 '17 at 17:24
Try something like this:
find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note. =~ matches regular expression. It matches f i l e 1.â Valentin B
Oct 16 '17 at 17:28
Try something like this:
find . -type f -exec sh -c 'for file; do [[ $file == *file1* || $file == *file2* ]] && echo "Got $file"; done' _ +. Another note. =~ matches regular expression. It matches f i l e 1.â Valentin B
Oct 16 '17 at 17:28
add a comment |Â
up vote
0
down vote
Just fixing the syntax
ROOTPATH="/path/here"
p1="file1"
p2="file2"
for file in `find $ROOTPATH -type f`;
do
if [[ $file =~ $p1 ]];
then
echo "got p1"
elif [[ $file =~ $p2 ]];
then
echo "got p2"
else
echo "got nothing"
fi
done
Arrangement
- Removing spaces in variable assignment
-typeinstead oftype- Removed
"in variable$filebecause of[[usage - Added space (
[[ $file&p1 ]]) - Adding
fi&doneterminations
As commented in comments, please be aware of looping with find command.
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
add a comment |Â
up vote
0
down vote
Just fixing the syntax
ROOTPATH="/path/here"
p1="file1"
p2="file2"
for file in `find $ROOTPATH -type f`;
do
if [[ $file =~ $p1 ]];
then
echo "got p1"
elif [[ $file =~ $p2 ]];
then
echo "got p2"
else
echo "got nothing"
fi
done
Arrangement
- Removing spaces in variable assignment
-typeinstead oftype- Removed
"in variable$filebecause of[[usage - Added space (
[[ $file&p1 ]]) - Adding
fi&doneterminations
As commented in comments, please be aware of looping with find command.
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Just fixing the syntax
ROOTPATH="/path/here"
p1="file1"
p2="file2"
for file in `find $ROOTPATH -type f`;
do
if [[ $file =~ $p1 ]];
then
echo "got p1"
elif [[ $file =~ $p2 ]];
then
echo "got p2"
else
echo "got nothing"
fi
done
Arrangement
- Removing spaces in variable assignment
-typeinstead oftype- Removed
"in variable$filebecause of[[usage - Added space (
[[ $file&p1 ]]) - Adding
fi&doneterminations
As commented in comments, please be aware of looping with find command.
Just fixing the syntax
ROOTPATH="/path/here"
p1="file1"
p2="file2"
for file in `find $ROOTPATH -type f`;
do
if [[ $file =~ $p1 ]];
then
echo "got p1"
elif [[ $file =~ $p2 ]];
then
echo "got p2"
else
echo "got nothing"
fi
done
Arrangement
- Removing spaces in variable assignment
-typeinstead oftype- Removed
"in variable$filebecause of[[usage - Added space (
[[ $file&p1 ]]) - Adding
fi&doneterminations
As commented in comments, please be aware of looping with find command.
edited Oct 16 '17 at 18:13
answered Oct 16 '17 at 16:30
tachomi
3,47431134
3,47431134
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
add a comment |Â
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
@JeffSchaller Thanks for the heads up
â tachomi
Oct 16 '17 at 18:13
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398446%2fmultiple-string-comparison-in-bash%23new-answer', 'question_page');
);
Post as a guest
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
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
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
and a possible copy/paste typo regarding the match for $p2 reporting "got p1"; and the extra surrounding spaces on the p1/p2 assignments
â Jeff Schaller
Oct 16 '17 at 16:06
1
you might use shellcheck.net as a first-pass through the syntax errors, then include any remaining errors in your Question here
â Jeff Schaller
Oct 16 '17 at 16:07
1
See Why is looping over find's output bad practice?
â don_crissti
Oct 16 '17 at 16:21