Sponsored Content
Top Forums UNIX for Advanced & Expert Users When is a _function_ not a _function_? Post 302832031 by wisecracker on Friday 12th of July 2013 02:00:34 PM
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...
 
echo(3XCURSES)						  X/Open Curses Library Functions					    echo(3XCURSES)

NAME
echo, noecho - enable/disable terminal echo SYNOPSIS
cc [ flag... ] file... -I /usr/xpg4/include -L /usr/xpg4/lib -R /usr/xpg4/lib -lcurses [ library... ] c89 [ flag... ] file... -lcurses [ library... ] #include <curses.h> int echo(void); int noecho(void); DESCRIPTION
The echo() function enables Echo mode for the current screen. The noecho() function disables Echo mode for the current screen. Initially, curses software echo mode is enabled and hardware echo mode of the tty driver is disabled. The echo() and noecho() functions control soft- ware echo only. Hardware echo must remain disabled for the duration of the application, else the behavior is undefined. RETURN VALUES
Upon successful completion, these functions return OK. Otherwise, they return ERR. ERRORS
No errors are defined. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
getch(3XCURSES), getstr(3XCURSES), initscr(3XCURSES), libcurses(3XCURSES), scanw(3XCURSES), attributes(5), standards(5) SunOS 5.10 5 Jun 2002 echo(3XCURSES)
All times are GMT -4. The time now is 11:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy