Re: Some bash question



On Saturday, 06 augustus, 2011 bij 15:06:04u +0200, Houghi schreef:

By re-writing my scripts I cam across something I use a lot:

#!/bin/bash
FOO ()
{
echo "BAR"
}
FOO

Two questions:

1) How is this called?

The function FOO is called by the use of FOO in the main program or other function.

2) The () could contain something

No, it is forbidden by the law of Bourne to contain anything ;)

How and where is this explained?

Should be in the manual page for sh or bash. Or look at a well written book
on shell programming.

Incidentally, you should collect all your commonly used functions into a library file
and then source that from the individual scripts.

eg something like

script_functions="${script_dir}/functions.sh"

if [ -r "${script_functions}" ]
then
. "${script_functions}"
else
/bin/echo -e "\007%ERROR - script functions file cannot be read!" >&2
exit 1
fi


Thus when you need to add some bug fix or enhancement to a function, you only have
to change it in one file and not tens of files.

And my personal opinion is that for true portability, best to keep scripts as
Bourne shell compliant and not use bashisms.

What you do on your machine is your affair, but you appear to be asking for some
advice, and other readers hopefully are also interested.
.



Relevant Pages

  • Re: Insert with response
    ... FooBar, there's no way and no need to put them in synch. ... column in the foo table to 250 calumns in the bar table. ... set statistics time off ...
    (microsoft.public.sqlserver.programming)
  • Re: Magic function
    ... processing objects created in root at depth 3 ... root obj2 at depth 3 ... processing objects created in foo at depth 2 ... processing objects created in bar at depth 0 ...
    (comp.lang.python)
  • Re: Any Clojure users here?
    ... (let [my-list (foo x)] ... If the stack depth is N when you call baz, it's only N+3 in bar and N+2 ... It is N+3 in map, but map creates a lazy sequence object rather than ... a Java method call. ...
    (comp.lang.lisp)
  • Re: Closing parenthesis in functions definition followed by its call
    ... return [bar, foo]; ... Evaluating the ArrayLiteral, the identifiers `foo` and `bar` must be resolved. ... The identifier `foo` is resolved on the VO, however `bar` cannot be resolved to an identifier and so a ReferenceError is produced. ...
    (comp.lang.javascript)
  • Re: from __future__ import absolute_import ?
    ... foo not in bar ... Unfortunately this is a side effect of using the os's directory structure to represent a python "package" structure. ...
    (comp.lang.python)