Multiple String Comparison in Bash

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question




















  • 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














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?







share|improve this question




















  • 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












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?







share|improve this question












 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?









share|improve this question











share|improve this question




share|improve this question










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
















  • 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










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.






share|improve this answer






















  • 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 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


















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


  • -type instead of type

  • Removed " in variable $file because of [[ usage

  • Added space ([[ $file & p1 ]])

  • Adding fi & done terminations

As commented in comments, please be aware of looping with find command.






share|improve this answer






















  • @JeffSchaller Thanks for the heads up
    – tachomi
    Oct 16 '17 at 18:13










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer






















  • 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 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















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.






share|improve this answer






















  • 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 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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

















  • 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 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
















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













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


  • -type instead of type

  • Removed " in variable $file because of [[ usage

  • Added space ([[ $file & p1 ]])

  • Adding fi & done terminations

As commented in comments, please be aware of looping with find command.






share|improve this answer






















  • @JeffSchaller Thanks for the heads up
    – tachomi
    Oct 16 '17 at 18:13














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


  • -type instead of type

  • Removed " in variable $file because of [[ usage

  • Added space ([[ $file & p1 ]])

  • Adding fi & done terminations

As commented in comments, please be aware of looping with find command.






share|improve this answer






















  • @JeffSchaller Thanks for the heads up
    – tachomi
    Oct 16 '17 at 18:13












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


  • -type instead of type

  • Removed " in variable $file because of [[ usage

  • Added space ([[ $file & p1 ]])

  • Adding fi & done terminations

As commented in comments, please be aware of looping with find command.






share|improve this answer














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


  • -type instead of type

  • Removed " in variable $file because of [[ usage

  • Added space ([[ $file & p1 ]])

  • Adding fi & done terminations

As commented in comments, please be aware of looping with find command.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • @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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)