help with bash functions Unix

help with bash functions Unix

Post by Billy Patto » Sat, 18 Dec 2004 01:55:35



if i have function:

   func_a ()
   {
     printf "  I'm in '%s'\n" how_do_I_get_function_name;
     ...
   }

How do I get the function name.

I'll be writing a lot of these, lots of cutting and pasting.
It will be VERY easy to forget to change the name of the function
nested within the function.
So how do I get the name?

    ___  _ ____       ___       __  __
   / _ )(_) / /_ __  / _ \___ _/ /_/ /____  ___
  / _  / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/   \_,_/\__/\__/\___/_//_/
            /___/
Texas Instruments ASIC Circuit Design Methodology Group

 
 
 

help with bash functions Unix

Post by William Par » Sat, 18 Dec 2004 02:46:26



> if i have function:

>    func_a ()
>    {
>      printf "  I'm in '%s'\n" how_do_I_get_function_name;
>      ...
>    }

> How do I get the function name.

> I'll be writing a lot of these, lots of cutting and pasting.
> It will be VERY easy to forget to change the name of the function
> nested within the function.
> So how do I get the name?

man bash
/funcname

--

Open Geometry Consulting, Toronto, Canada
Linux solution for data processing.

 
 
 

help with bash functions Unix

Post by Chris F.A. Johnso » Sat, 18 Dec 2004 02:50:06



> if i have function:

>    func_a ()
>    {
>      printf "  I'm in '%s'\n" how_do_I_get_function_name;
>      ...
>    }

> How do I get the function name.

> I'll be writing a lot of these, lots of cutting and pasting.
> It will be VERY easy to forget to change the name of the function
> nested within the function.
> So how do I get the name?

    In bash, there is the variable, FUNCNAME:

func()
{
  printf "FUNCNAME = %s" $FUNCNAME

Quote:}

    I have never needed it; why do you?

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

help with bash functions Unix

Post by Billy Patto » Sat, 18 Dec 2004 03:19:42




>> if i have function:

>>    func_a ()
>>    {
>>      printf "  I'm in '%s'\n" how_do_I_get_function_name;
>>      ...
>>    }

>> How do I get the function name.

>> I'll be writing a lot of these, lots of cutting and pasting.
>> It will be VERY easy to forget to change the name of the function
>> nested within the function.
>> So how do I get the name?

>    In bash, there is the variable, FUNCNAME:

> func()
> {
>  printf "FUNCNAME = %s" $FUNCNAME
> }

>    I have never needed it; why do you?

   abc ()
   {
     outdir=$OUTDIR/$FUNCNAME;

Just one way.  For each task I will have a function by the same name  It will
also be part of the output directory.

The more I can cut/paste without having to edit, the better off I am.

Quote:

> --
>    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
>    ===================================================================
>    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
>    and may be copied under the terms of the GNU General Public License

    ___  _ ____       ___       __  __
   / _ )(_) / /_ __  / _ \___ _/ /_/ /____  ___
  / _  / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/   \_,_/\__/\__/\___/_//_/
            /___/
Texas Instruments ASIC Circuit Design Methodology Group

 
 
 

help with bash functions Unix

Post by Ed Morto » Sat, 18 Dec 2004 04:11:41


<snip>

Quote:> The more I can cut/paste without having to edit, the better off I am.

Or: The more I can cut/paste without having to edit, the WORSE off I am.

I know that sounds facetious, but all I'm trying to point out is that if
you're doing a lot of cutting and pasting without editting, then you're
duplicating a lot of code which leads to maintenance problems in future
when you want to make a small tweak (or major rework!) and find you have
to make the same change in multiuple locations.

So, just think about it....

Also consider if there'd be any value in these functions being stored in
separate files, e.g. so you can call them from the command line or from
different scripts. If so, you can use `basename $0` (which isn't
bash-specific) or it's ilk instead of $FUNCNAME to get the "function" name.

        Ed.

 
 
 

help with bash functions Unix

Post by Stephane CHAZELA » Sat, 18 Dec 2004 19:00:17


2004-12-16, 12:19(-06), Billy Patton:
[...]
Quote:>    abc ()
>    {
>      outdir=$OUTDIR/$FUNCNAME;

> Just one way.  For each task I will have a function by the same name  It will
> also be part of the output directory.

[...]

You can use a function to define the function:

myfunction() {
  eval "
    $1() {
      funcname='$1'
      $2
    }"

Quote:}

Then:

myfunction abc '
  outdir=$OUTDIR/$funcname
  ...
'

But that won't make things easy if you need to use single quotes
in the code of your functions. Alternatively, you could do:

myfunction() {
  eval "
    $1() {
      funcname='$1'
      `cat`
    }"

Quote:}

myfunction abc << \EOF
  outdir=$OUTDIR/$funcname
  sed 's/#.*//' < "$infile" > "$outdir/$outfile"
EOF

--
Stephane

 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. How do you mount a tape drive

3. BASH function: does the definition order of the function matter?

4. Installing GNU make, etc.

5. Is a bash function a "function" or a "subroutine"?

6. Printer not recognized unless ON at power up

7. bash: function-defining function?

8. User administration question

9. bash function problem, unless su is used - please help!

10. Need help with function in bash

11. help: bash shell function

12. Please help with simple Bash shell function

13. Need help correcting this Bash function