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:
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
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:
yesss
1
['hehe.desktop', , 'end.desktop']
Expected output:
Code:
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']