Nested IF statement with Regex operator in a While loop, Stops the loop
Nested IF statement with Regex operator in a While loop, Stops the loop
(OP)
Is "=~" really a comparison operator? I had this question,
but somehow the tests that I did, showed that it is not an assignment operator. So it must be comparison operator?
Still unsure. The way it is acting in my code is kind of weird
I recently tried to nest If statement that contains regex operator,
it seems that it is correct statement, but somehow breaks the whole While loop without an error.
Removing the statement makes the While loop work as expected. I'm getting frustrated.
This is an extract from the code below:
The full code
Current output:
Expected output:
but somehow the tests that I did, showed that it is not an assignment operator. So it must be comparison operator?
Still unsure. The way it is acting in my code is kind of weird
I recently tried to nest If statement that contains regex operator,
it seems that it is correct statement, but somehow breaks the whole While loop without an error.
Removing the statement makes the While loop work as expected. I'm getting frustrated.
This is an extract from the code below:
CODE --> bash
# # This is the problematic portion of code # Remove this if statement and the code works great if [[ ${BASH_REMATCH[1]} =~ "desktop" ]]; then echo yesss fi
The full code
CODE --> bash
#!/bin/bash function reconstructArray(){ local TEXT_TO_PARSE=$1; local ARRAY_ELEMENT=$2; #TODO # MANIPULATION_MODE # Remove, Add local MANIPULATION_MODE=$3; # Position in the array # Search In Array items and remove local i=0; local REGEX_PATTERN="'.([^']*)'"; local NEW_ARRAY="["; local NEW_ARRAY+="'hehe.desktop', "; while [[ ${TEXT_TO_PARSE} =~ (${REGEX_PATTERN}) ]]; do # This is the problematic portion of code # Remove this and the code works great if [[ ${BASH_REMATCH[1]} =~ "desktop" ]]; then echo yesss fi # Loop Increment i=$((i+1)); echo "$i ${BASH_REMATCH[1]}"; # First element of the array if [ $i -eq 1 ] then NEW_ARRAY+="${BASH_REMATCH[1]}"; else NEW_ARRAY+=", ${BASH_REMATCH[1]}"; fi # nth element of the array if [ $i -eq 5 ] then NEW_ARRAY+=", 'testing'"; fi TEXT_TO_PARSE=${TEXT_TO_PARSE##*${BASH_REMATCH[1]}} done # Ending element of the array NEW_ARRAY+=", 'end.desktop'"; NEW_ARRAY+="]"; echo $NEW_ARRAY } reconstructArray "$(gsettings get org.gnome.shell favorite-apps)";
Current output:
CODE --> text
yesss 1 ['hehe.desktop', , 'end.desktop']
Expected output:
CODE --> text
yesss 1 'firefox.desktop' yesss 2 'google-chrome.desktop' yesss 3 'thunderbird.desktop' yesss 4 'org.gnome.Nautilus.desktop' yesss 5 'rhythmbox.desktop' yesss 6 'libreoffice-writer.desktop' yesss 7 'org.gnome.Software.desktop' yesss 8 'yelp.desktop' yesss 9 'ubuntu-amazon-default.desktop' ['hehe.desktop', 'firefox.desktop', 'google-chrome.desktop', 'thunderbird.desktop', 'org.gnome.Nautilus.desktop', 'rhythmbox.desktop', 'testing', 'libreoffice-writer.desktop', 'org.gnome.Software.desktop', 'yelp.desktop', 'ubuntu-amazon-default.desktop', 'end.desktop']
RE: Nested IF statement with Regex operator in a While loop, Stops the loop
Your problem is caused by =~'s behavior :
So when you use =~ again, that will also populate BASH_REMATCH with its own capture groups, overwriting BASH_REMATCH's previous content. So when later in the code you try to use it in more conditional expressions, those will work with the already changed value.
So either
Feherke.
feherke.github.io