Sponsored Content
Top Forums Shell Programming and Scripting How to make nested function local? Post 302999533 by chebarbudo on Thursday 22nd of June 2017 09:07:51 AM
Old 06-22-2017
How to make nested function local?

Hi,

If I declare a function inside another function, it overwrites any previously declared function with the same name. This is NOT what I want.

Example:
Code:
#!/bin/bash

_test() { echo test; }

_myf() {
    # I'm using the same name as the other function.
    _test() { echo local test; }
    # I'm expecting this call to print "local test"
    _test
}

# I'm expecting this call to print "test"
_test
# I'm calling myf hopping it will print "local test" but leave function "_test" unchanged.
_myf
# I'm expecting this call to still print "test" but function _test has been irredeemably changed.
_test

What I hop to get is:
Code:
test
local test
test

What I get is:
Code:
test
local test
local test

How can I make sure that any function I declare inside a function stays local to the later function?

Thanks for you help.
Santiago
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make variables in script function local?

Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a... (5 Replies)
Discussion started by: alex_5161
5 Replies

2. Shell Programming and Scripting

How to execute local function in awk

Hi All, Can you please tell me how to execute local function written in a shell script with awk. i tried with system command but its giving an error. (1 Reply)
Discussion started by: krishna_gnv
1 Replies

3. UNIX for Dummies Questions & Answers

How to call a local function within Awk

Hi, I have the following statement which parses a string for me and prints it out: l_comp="dc000.runksh.test.ksh| $g_sql/dc0000.runksh_test.sql|new.dat|control.ctl" echo $l_comp | awk -F"|" '{ for ( i = 1; i <= NF; i++) { print $i; } } ' Rather then printing the data, I would like to... (5 Replies)
Discussion started by: CAGIRL
5 Replies

4. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

5. UNIX for Dummies Questions & Answers

make - foreach function

I wrote the following Makefile: dirs := a b c d files := $(foreach dir,$(dirs),$(wildcard $(dir)/*)) .PHONY: all all: touch $(files) The first two lines are taken from GNU make tutorial, Section 8.5 The foreach Function. I would expect the recipe touch $(files) to be... (2 Replies)
Discussion started by: ybelenky
2 Replies

6. Programming

Thread function local variables

As I know threads share the memory. But, what about the local variables in the thread function? if i call multiple threads would they allocate seperate local variables for themselves? like thread_func() { int i, j; string... } Are the above local variables defined for each of... (1 Reply)
Discussion started by: saman_glorious
1 Replies

7. Programming

Returning local string value from a function in C

Hi, If I have a code like this, what are the potential problems do you see? const char* const retString() { return "hello"; /* string literal */ } My questions are: a) Since the string literal which is already a constant read only data (cannot be... (4 Replies)
Discussion started by: royalibrahim
4 Replies

8. Shell Programming and Scripting

/usr/local/bin/expr function not working

Legends, I am not able to set "expr" function in ksh script. Below is the sample code i used, and output is as "Syntax error" Please help me to come out of it. OUTPUT (9 Replies)
Discussion started by: sdosanjh
9 Replies

9. Programming

Local variable in a C function is not getting created in stack when its compiled with GCC

Hi, I am working in UEFI EDK2 Bios source. We created a platform related new package in the EDK2 source. I find a strange issue with the platform related code we added. When I did source level debugging I noticed the local variable in a C function is not getting created in stack when its... (6 Replies)
Discussion started by: Divya R
6 Replies

10. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies
local(n)							    [incr Tcl]								  local(n)

__________________________________________________________________________________________________________________________________________________

NAME
local - create an object local to a procedure SYNOPSIS
itcl::local className objName ?arg arg ...? _________________________________________________________________ DESCRIPTION
The local command creates an [incr Tcl] object that is local to the current call frame. When the call frame goes away, the object is auto- matically deleted. This command is useful for creating objects that are local to a procedure. As a side effect, this command creates a variable named "itcl-local-xxx", where xxx is the name of the object that is created. This vari- able detects when the call frame is destroyed and automatically deletes the associated object. EXAMPLE
In the following example, a simple "counter" object is used within the procedure "test". The counter is created as a local object, so it is automatically deleted each time the procedure exits. The puts statements included in the constructor/destructor show the object coming and going as the procedure is called. itcl::class counter { private variable count 0 constructor {} { puts "created: $this" } destructor { puts "deleted: $this" } method bump {{by 1}} { incr count $by } method get {} { return $count } } proc test {val} { local counter x for {set i 0} {$i < $val} {incr i} { x bump } return [x get] } set result [test 5] puts "test: $result" set result [test 10] puts "test: $result" puts "objects: [itcl::find objects *]" KEYWORDS
class, object, procedure itcl local(n)
All times are GMT -4. The time now is 01:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy