×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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:

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

Hi

Your problem is caused by =~'s behavior :

Quote (man bash)

Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH.
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
  • Change the condition to pattern matching : [[ ${BASH_REMATCH[1]} == \'*.desktop\' ]]
  • Before that condition store the value for later use in a variable : initial_rematch=( "${BASH_REMATCH[@]}" ), then change further conditions to use that variable instead

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close