You are not logged in.

#1 2014-05-29 12:32:27

xvzf
Member
Registered: 2014-05-18
Posts: 44

Bash script, syntax error

Hello,

I think it's just a simple issue, but I don't know much about BASH scripts, normaly I develop in C/C++ but I don't like these Makefiles (don't blame me!!).

Here's my simple code:

#!/bin/bash

####                   ####
#  INFINITY BUILD SYSTEM  #
####                   ####



#	Project defines
export AVR_CPPFLAGS='-O3 -mmcu=atmega32u4 -I./inc/'
export AVR_LDFLAGS=''
export PROJECT_SOURCES=''
export PROJECT_HOME=.

#########

# Internal defines
export AVR_CPP='avr-c++'
export AVR_LD='avr-ld'
export AVR_OBJCOPY=avr-objcpy
export PROJECT_SRC_DIR=${PROJECT_HOME}/src/
export PROJECT_INC_DIR=${PROJECT_HOME}/inc/


function compile {
 

	for i in $PROJECT_SOURCES  do
		$AVR_CPP $AVR_CPPFLAGS $to_compile -o ${to_compile}.o
		if [ $? -eq 0 ]	then
			echo “$to_compile:Compiled successfully”
		else
			echo “$to_compile: FAILED, see log above!”
			exit
		fi 
	done

}

it says:

./build.sh: line 29: syntax error near unexpected token `avr-cpp'
./build.sh: line 29: `		avr-cpp $AVR_CPPFLAGS $to_compile -o ${to_compile}.o'

Any help would be nice! Thanks!

Offline

#2 2014-05-29 12:56:52

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,770
Website

Re: Bash script, syntax error

You're missting a semicolon or newline before the 'do' on line 28.

EDIT: and what is "$to_compile"?  It is never defined.  I think you mean "$i".

Last edited by Trilby (2014-05-29 12:58:19)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2014-05-29 12:57:19

oliver
Member
Registered: 2007-12-12
Posts: 433

Re: Bash script, syntax error

It needs a semi-colon before the 'do' (or drop the 'do' to the next line)

Either one or the other:

for i in whatever; do
  stuff
done
for i in whatever
do
  stuff
done

You'll have the same error with the 'if' statement too... it will need a semi-colon before the 'then'

if [ $? -eq 0 ]; then

Offline

#4 2014-05-29 12:59:14

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,770
Website

Re: Bash script, syntax error

Also, while not an error, it would be better to use the bash builtin [[ ]] rather than the binary [ particularly since you are envoking bash explicitly.

Last edited by Trilby (2014-05-29 12:59:46)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#5 2014-05-29 13:36:19

oliver
Member
Registered: 2007-12-12
Posts: 433

Re: Bash script, syntax error

Trilby wrote:

EDIT: and what is "$to_compile"?  It is never defined.  I think you mean "$i".

On a similar point, a couple of nice options for bash scripts are:

set -u
set -e

The first one will stop the script if there are unbound variables and the second will stop on an error.  Both can be very useful

#!/usr/bin/env bash

set -e 
set -u

# rest of your script etc

Offline

#6 2014-05-29 15:39:38

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,097
Website

Re: Bash script, syntax error

oliver wrote:
Trilby wrote:

EDIT: and what is "$to_compile"?  It is never defined.  I think you mean "$i".

On a similar point, a couple of nice options for bash scripts are:

set -u
set -e

The first one will stop the script if there are unbound variables and the second will stop on an error.  Both can be very useful

#!/usr/bin/env bash

set -e 
set -u

# rest of your script etc

Sorry, this is crap advice. set -e has surprising and inconsistent behavior. set -u causes you to code around problems that it creates. The combination of the two flags is a nightmare. There's nothing "similar" about your suggestion and what the OP asked, or what Trilby responded with.

Last edited by falconindy (2014-05-29 15:40:37)

Offline

#7 2014-05-29 17:34:02

oliver
Member
Registered: 2007-12-12
Posts: 433

Re: Bash script, syntax error

falconindy wrote:

Sorry, this is crap advice. set -e has surprising and inconsistent behavior. set -u causes you to code around problems that it creates. The combination of the two flags is a nightmare. There's nothing "similar" about your suggestion and what the OP asked, or what Trilby responded with.

From the link you posted:
rking's personal recommendation is to go ahead and use set -e, but beware of possible gotchas. It has useful semantics, so to exclude it from the toolbox is to give into FUD.

The reason I mentioned set -u is because it would have been extremely clear why the first line in the loop gave odd results rather than fundamentally changing what OP thought it was doing.  Therefore the similarity was that Trilby mentioned an unbound variable and I posted a method to check for that (however flawed and basic it might be in complex examples)

In saying this, in reading through the programming subforum I certainly appreciate that your programming skills far exceed mine so I'll certainly take your advice/concern on-board

Last edited by oliver (2014-05-29 17:39:13)

Offline

#8 2014-05-30 16:16:11

xvzf
Member
Registered: 2014-05-18
Posts: 44

Re: Bash script, syntax error

Great guys! Thanks for all the help!

The script can be found at github here:
http://github.com/xvzf/Infinity under the name "ibs.sh"

Again, thank for your help, I've never done anything in BASH before... Now I really like it, i have to say big_smile

Offline

#9 2014-05-30 17:32:44

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,770
Website

Re: Bash script, syntax error

xvzy, you may want to peruse Greg's wiki - it is a great reference and learning aid for bash.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

Board footer

Powered by FluxBB