Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested IF statement with Regex operator in a While loop, Stops the loop 1

Status
Not open for further replies.

boqsc

Technical User
Jan 28, 2020
4
0
0
LT
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:
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']


 
Hi

Your problem is caused by [tt]=~[/tt]'s behavior :
man bash said:
Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH.
So when you use [tt]=~[/tt] again, that will also populate [tt]BASH_REMATCH[/tt] with its own capture groups, overwriting [tt]BASH_REMATCH[/tt]'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
[ul]
[li]Change the condition to pattern matching : [tt][teal][[[/teal] [navy]${BASH_REMATCH[1]}[/navy] [teal]==[/teal] \'[teal]*.[/teal]desktop\' [teal]]][/teal][/tt][/li]
[li]Before that condition store the value for later use in a variable : [tt][navy]initial_rematch[/navy][teal]=([/teal] [green]"${BASH_REMATCH[@]}"[/green] [teal])[/teal][/tt], then change further conditions to use that variable instead[/li]
[/ul]

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top