×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!
  • Students Click Here

*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

Using IF statement's Regex comparison operator in a C Style For Loop

Using IF statement's Regex comparison operator in a C Style For Loop

Using IF statement's Regex comparison operator in a C Style For Loop

(OP)
I'm trying to convert a While statement into a For Loop, but I'm not very successful and unsure if it is even possible.

Error output syntax error: operand expected

CODE --> text

./script.bash: line 9: ((: [[ ['firefox.desktop', 'google-chrome.desktop', 'thunderbird.desktop', 'org.gnome.Nautilus.desktop', 'rhythmbox.desktop', 'libreoffice-writer.desktop', 'org.gnome.Software.desktop', 'yelp.desktop', 'ubuntu-amazon-default.desktop'] =~ ('.([^']*)') ]]: syntax error: operand expected (error token is "[[ ['firefox.desktop', 'google-chrome.desktop', 'thunderbird.desktop', 'org.gnome.Nautilus.desktop', 'rhythmbox.desktop', 'libreoffice-writer.desktop', 'org.gnome.Software.desktop', 'yelp.desktop', 'ubuntu-amazon-default.desktop'] =~ ('.([^']*)') ]]") 

The For loop that contains error

CODE --> bash

#!/bin/bash
TEXT_TO_PARSE=$(gsettings get org.gnome.shell favorite-apps)
REGEX_PATTERN="'.([^']*)'"
for ((i=0; [[ ${TEXT_TO_PARSE} =~ (${REGEX_PATTERN}) ]]; i++))
do
    echo "$i ${BASH_REMATCH[1]}"
    TEXT_TO_PARSE=${TEXT_TO_PARSE##*${BASH_REMATCH[1]}}
done 


The While loop that works,
but is a While Loop and needs conversion to For Loop, to make it more readable.

CODE --> bash

#!/bin/bash
# Parsing and reconstructing array

TEXT_TO_PARSE=$(gsettings get org.gnome.shell favorite-apps)
function reconstructArray(){
	REGEX_PATTERN="'.([^']*)'"
	BEGINING="[";
	BEGINING+="'hehe.desktop', ";
	i=0;
	while [[ ${TEXT_TO_PARSE} =~ (${REGEX_PATTERN}) ]]; do
		
		# Loop Increment
		i=$((i+1)) && echo "$i ${BASH_REMATCH[1]}"

		# First element of the array
		if [ $i -eq 1 ]
		then
		  BEGINING+="${BASH_REMATCH[1]}";
		else 
		  BEGINING+=", ${BASH_REMATCH[1]}";
		fi
		
		# nth element of the array
		if [ $i -eq 5 ]
		then
		  BEGINING+=", 'testing'";
		fi

		TEXT_TO_PARSE=${TEXT_TO_PARSE##*${BASH_REMATCH[1]}}
	done
		# Ending element of the array
		BEGINING+=", 'end.desktop'";
		BEGINING+="]";
		echo $BEGINING
}
reconstructArray; 

RE: Using IF statement's Regex comparison operator in a C Style For Loop

Hi

I am afraid that is not possible :

Quote (man bash)

for (( expr1 ; expr2 ; expr3 )) ; do list ; done
(...)
The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero.

So Bash expects an arithmetic expression there. Using [[ .. ]] there would be like

CODE --> command line

master # echo $(( a == b ))
1

# vs

master # echo $(( [[ a == b ]] ))
bash: [[ a == b ]] : syntax error: operand expected (error token is "[[ a == b ]] ") 

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