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
The For loop that contains error
The While loop that works,
but is a While Loop and needs conversion to For Loop, to make it more readable.
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;