When is a _function_ not a _function_?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users When is a _function_ not a _function_?
Prev   Next
# 1  
Old 07-12-2013
When is a _function_ not a _function_?

For a starter I know the braces are NOT in the code...

Consider these code snippets:-
Code:
#!/bin/bash --posix
x=0
somefunction()
if [ $x = 0 ]
then
	echo "I am here."
fi
# somefunction





#!/bin/bash --posix
x=0
somefunction()
if [ $x = 0 ]
then
	echo "I am here."
fi
somefunction





#!/bin/bash --posix
x=0
somefunction()
echo "Why does this crash?"
if [ $x = 0 ]
then
	echo "I am here."
fi
somefunction

Now using OSX 10.7.5 and bash here is the result:-
Code:
Last login: Fri Jul 12 18:41:45 on ttys000
AMIGA:barrywalker~> ./func.sh
AMIGA:barrywalker~> ./func.sh
I am here.
AMIGA:barrywalker~> ./func.sh
./func.sh: line 4: syntax error near unexpected token `echo'
./func.sh: line 4: `echo "Why does this crash?"'
AMIGA:barrywalker~> _

Why do the first two snippets work as predicted, (although without the braces), yet the third crashes out with the error report?
What is going on?

Can someone explain what is going on?

Many thanks...
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question