LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2011, 03:51 PM   #1
HalfEmptyHero
LQ Newbie
 
Registered: Aug 2011
Distribution: Arch
Posts: 6

Rep: Reputation: Disabled
What is wrong with my bash script?


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

Last edited by HalfEmptyHero; 08-02-2011 at 03:54 PM.
 
Old 08-02-2011, 03:56 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

you are simply missing a 'then'. Try this:
Code:
elif [[ -n "$DELETE_ENTRY" ]];then
It should work now.

Welcome to LQ.
 
1 members found this post helpful.
Old 08-02-2011, 04:03 PM   #3
HalfEmptyHero
LQ Newbie
 
Registered: Aug 2011
Distribution: Arch
Posts: 6

Original Poster
Rep: Reputation: Disabled
Doh! I read over the thing so many times and thought I checked every if, then, and fi. Sometimes it just takes another pair of eyes I guess. Thanks a lot man. LQ is proving to be great already.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What am I doing wrong in this bash script ? bashprog Programming 4 10-10-2010 07:19 PM
(BASH) Works on the command line, hangs as a script -- what's wrong? SilversleevesX Programming 17 08-08-2010 10:19 PM
What Is Wrong With This Bash Script File? George2 Programming 24 05-11-2006 08:29 PM
My first BASH script, what's wrong with it? szf2 Linux - Newbie 2 11-12-2003 01:43 PM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration