I've been trying out Gnome-Shell on Fedora 15 lately, and came across a cool extension for Gnome-Shell that automatically opens programs in the workspace of your choice when you open it. The only way to configure that is through gsettings either from the command-line or through dconf-editor. I decided this would be the perfect time for my to practice my bash scripting, and set out to write my first bash script. Everything works, it might not be the best way to do it, but it does work. The only problem I am running into is a syntax error near unexpected token `fi'
I tracked it down, and if I comment out those lines of code everything runs smoothly, but I can't figure out why it's happening. I'm sure I overlooked something simple, so I'm hoping you guys can help me out. I apologize if the formatting/commenting is kind of weird.
Code:
#!/bin/bash
# ---------------------Variables--------------------------- #
CURRENT_SETTINGS=$( gsettings get org.gnome.shell.extensions.auto-move-windows application-list )
DELETE_ENTRY= # The argument passed if -d is used
FILENAME= # The argument passed if -f is u
RESET_SETTINGS=0
SHOW_SETTINGS=0
# --------------------------------------------------------- #
# ----------------Command-Line Options--------------------- #
optstring=d:f:r
while getopts $optstring opt
do
case $opt in
d) DELETE_ENTRY=$OPTARG ;;
f) FILENAME=$OPTARG ;;
r) RESET_SETTINGS=$(( $RESET_SETTINGS + 1 )) ;;
s) RESET_SETTINGS=$(( $SHOW_SETTINGS + 1 )) ;;
esac
done
# --------------------------------------------------------- #
# ------------------------setIt---------------------------- #
# Sets the gsettings to $NEW_LIST
set_it ()
{
gsettings set org.gnome.shell.extensions.auto-move-windows application-list "$NEW_LIST"
}
# ---------------------change_it--------------------------- #
# Checks the current gsettings for the new entry #
# Either adds a new entry or updates current one #
# Returns $NEW_LIST
change_it ()
{
if echo $CURRENT_SETTINGS | grep -q $DESKTOP_FILE
# if the current gsettings contain a listing for the .desktop file
then
NEW_LIST=$( echo "$CURRENT_SETTINGS" | sed "s/$DESKTOP_FILE:[0-9]/$DESKTOP_FILE:$WORKSPACE/")
# update the workspace for the current entry
else
if echo $CURRENT_SETTINGS | grep -q "desktop"
# if the CURRENT_SETTINGS contains desktop (there is already a desktop entry)
then
NEW_LIST=$( echo "$CURRENT_SETTINGS" | sed "s/']/',\ '$DESKTOP_FILE:$WORKSPACE']/" )
# add a comma and space seperating it from the previous entry and add new entry
else
# if no entries exist add the new entry
NEW_LIST="['$DESKTOP_FILE:$WORKSPACE']"
fi
fi
#set the gsettings entry to $NEW_LIST
set_it
}
if [ $RESET_SETTINGS -eq 1 ]
then
gsettings reset org.gnome.shell.extensions.auto-move-windows application-list
elif [[ ! -n "$FILENAME" && ! -n "$DELETE_ENTRY" ]]
then
DESKTOP_FILE=$( cd /usr/share/applications/ && grep -I *.desktop -d read -l -e "Exec=$1" )
# grep checks all the .desktop files in /usr/share/applications for the name of the executable
if echo $DESKTOP_FILE | grep -q ' ' #if grep returns more than one .desktop files
then
DESKTOP_FILE=$( cd /usr/share/applications/ && grep -I *.desktop -d read -l -e "Exec=$1 " )
# grep adds a space to the end of it's search (fixes evolution for example)
fi
WORKSPACE=$2 #$WORKSPACE becomes the 2nd argument on the command line
change_it
set_it
elif [[ -n "$FILENAME" ]]
then
DESKTOP_FILE=$FILENAME
WORKSPACE=$3
change_it
set_it
elif [[ -n "$DELETE_ENTRY" ]]
if ! echo $DELETE_ENTRY | grep -q "\.desktop" # if $DELETE_ENTRY is not a .desktop file
then # find the .desktop file corresponding to the executable
DELETE_ENTRY=$( cd /usr/share/applications/ && grep -I *.desktop -d read -l -e "Exec=$DELETE_ENTRY" )
fi
NEW_LIST=$( echo CURRENT_SETTINGS | sed "/s/'$DELETE_ENTRY:[0-9]'//" ) # Remove the entry
NEW_LIST=$( echo $( echo $( echo "$NEW_LIST" | sed "s/\[,\ /\[/" ) | sed "s/,\ ,/,/" ) | sed "s/,\ ]/]/" ) # Remove extra commas and spaces
set_it
fi
And if I comment out this section everything works.
Code:
elif [[ -n "$DELETE_ENTRY" ]]
if ! echo $DELETE_ENTRY | grep -q "\.desktop" # if $DELETE_ENTRY is not a .desktop file
then # find the .desktop file corresponding to the executable
DELETE_ENTRY=$( cd /usr/share/applications/ && grep -I *.desktop -d read -l -e "Exec=$DELETE_ENTRY" )
fi
NEW_LIST=$( echo CURRENT_SETTINGS | sed "/s/'$DELETE_ENTRY:[0-9]'//" ) # Remove the entry
NEW_LIST=$( echo $( echo $( echo "$NEW_LIST" | sed "s/\[,\ /\[/" ) | sed "s/,\ ,/,/" ) | sed "s/,\ ]/]/" ) # Remove extra commas and spaces
set_it
fi
Any help given is appreciated. It says the syntax error is on the very last line