8 Replies - 431 Views - Last Post: 07 May 2013 - 10:07 AM Rate Topic: -----

#1 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

BASH: Nested IF startement in bash returning an error

Posted 06 May 2013 - 03:05 PM

I have a nested if statement in which I am not seeing the error. Each time the script is run it returns an error stating:

rdscripts/backupimport.sh: line 66: syntax error near unexpected token `fi'
rdscripts/backupimport.sh: line 66: `fi'


line 66 is the final fi in the following code

#Hard Knock Radio
if [ -f /home/wazu/RadioDropBox/Politics/Hard\ Knock\ Radio/* ]; then\
numFiles=`ls /home/wazu/RadioDropBox/Politics/Hard\ Knock\ Radio/ | wc -l`
if [ numFiles -eq 1 ]; then
cp /home/wazu/RadioDropBox/Politics/Hard\ Knock\ Radio/* /home/wazu/RadioBackup/Politics/Hard\ Knock\ Radio/
rdimport --delete-cuts --to-cart=040003 --delete-source POLITICS /home/wazu/RadioDropBox/Politics/Hard\ Knock\ Radio/*
else
echo "Hard Knock Radio Folder contains more than one files, requires manual import" >> /home/wazu/logs/multiplefileerror.txt
fi
fi



As far as I can see this should not be creating an issue. What am I not seeing or what am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: BASH: Nested IF startement in bash returning an error

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: BASH: Nested IF startement in bash returning an error

Posted 06 May 2013 - 04:12 PM

if [ numFiles -eq 1 ]; then

should be
if [ $numFiles -eq 1 ]; then


I'd suggest using some variables.

WAZU_HOME=/home/wazu
DROPBOX=$WAZU_HOME/RadioDropBox
BACKUP=$WAZU_HOME/RadioBackup
PHKR=Politics/Hard\ Knock\ Radio

if [ -f $DROPBOX/$PHKR/* ]; then
	numFiles=`ls $DROPBOX/$PHKR/ | wc -l`
	if [ $numFiles -eq 1 ]; then
		cp $DROPBOX/$PHKR/* $BACKUP/$PHKR/
		rdimport --delete-cuts --to-cart=040003 --delete-source POLITICS $DROPBOX/$PHKR/*
	else
		echo "Hard Knock Radio Folder contains more than one files, requires manual import" >> $HOME/logs/multiplefileerror.txt
	fi
fi


However, using your -f test gives me a binary operator expected error with more than one file in a subdir.

If there is more than one file in the directory, is there any reason you can't iterate over them to back them up?
Was This Post Helpful? 1
  • +
  • -

#3 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: BASH: Nested IF startement in bash returning an error

Posted 06 May 2013 - 07:24 PM

I need it to create a log entryif there is more than one file. not backup or import until the situation can be assessed as there should only be one file in the dropbox at a time
Was This Post Helpful? 0
  • +
  • -

#4 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: BASH: Nested IF startement in bash returning an error

Posted 07 May 2013 - 08:46 AM

Ok so i resolved the issue with the missing "$" but am still receiving the same error.

my current code is:
ERROR:
rdscripts/backupimport.sh: line 71: syntax error near unexpected token `fi'
rdscripts/backupimport.sh: line 71: `fi'



VARIABLES:
2 #Create Variables
  3 WAZU_HOME=/home/wazu
  4 DROPBOX=$WAZU_HOME/RadioDropBox
  5 BACKUP=$WAZU_HOME/RadioBackUp
  6 



CODE:
62 #Hard Knock Radio
 63 if [ -f $DROPBOX/Politics/Hard\ Knock\ Radio/* ]; then\
 64 numFiles=`ls $DROPBOX/Politics/Hard\ Knock\ Radio/ | wc -l`
 65 if [ $numFiles -eq 1 ]; then
 66 cp $DROPBOX/Politics/Hard\ Knock\ Radio/* $BACKUP/Politics/Hard\ Knock\ Radio/
 67 rdimport --delete-cuts --to-cart=040003 --delete-source POLITICS $DROPBOX/Politics/Hard\ Knock\ Radio/*
 68 else
 69 echo "Hard Knock Radio Folder contains more than one files, requires manual import" >> $WAZU_HOME/logs/multiplefileerror.txt
 70 fi
 71 fi



EDIT: It works without the nested if statement (I know this because it didn't have it before and this is the only section of code to contain the nested if now because I am testing before adding it to every other if statement in case this happens) but I am going to need that in there for the script to do it's job correctly.

This post has been edited by TechnoBear: 07 May 2013 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: BASH: Nested IF startement in bash returning an error

Posted 07 May 2013 - 09:02 AM

Is there a particular reason you have a line continuation here?

if [ -f $DROPBOX/Politics/Hard\ Knock\ Radio/* ]; then\

Was This Post Helpful? 1
  • +
  • -

#6 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: BASH: Nested IF startement in bash returning an error

Posted 07 May 2013 - 09:41 AM

Well we appear to of found the issue. Thank you I feel like a numpty now. Now I am having a too many arguments issue but I am thinking that is not for this thread and that I can also deal with it myself.

My thinking is that -f is designed to check if a single file exists. I just need to work out the switch for if folder contents not empty. Which I think I can use the same switch I am using to check number of files.

Excuse me just rambling now for some reason. Thank you greatly though I never would of noticed that myself.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: BASH: Nested IF startement in bash returning an error

Posted 07 May 2013 - 10:02 AM

I would simply check that $DROPBOX/Politics/Hard\ Knock\ Radio/ is a directory and if it's a directory, get the number of files as you are now, and process accordingly.
Was This Post Helpful? 1
  • +
  • -

#8 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

Re: BASH: Nested IF startement in bash returning an error

Posted 07 May 2013 - 10:06 AM

Thank you.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5063
  • View blog
  • Posts: 26,437
  • Joined: 10-May 07

Re: BASH: Nested IF startement in bash returning an error

Posted 07 May 2013 - 10:07 AM

Why do you have a backslash on line 63, & not on line 65?

Also I would eliminate the 1st if, by simply outputing the error message to /dev/null. Then your ls can fail naturally, & you still only check if the output is equal to one or not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1