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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

boqsc

Technical User
Jan 28, 2020
4
LT
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:
./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
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.
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;
 
Hi

I am afraid that is not possible :
man bash said:
for (( expr1 ; expr2 ; expr3 )) ; do list ; done
[gray](...)[/gray]
The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero.​

So Bash expects an arithmetic expression there. Using [tt][[[/tt] .. [tt]]][/tt] there would be like
Code:
[blue]master #[/blue] echo $(( a == b ))
1

[gray]# vs[/gray]

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

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

Part and Inventory Search

Sponsor

Back
Top