Sponsored Content
Top Forums Shell Programming and Scripting [bash] reassigning referenced variables in functions Post 302305999 by ASGR on Friday 10th of April 2009 11:27:38 AM
Old 04-10-2009
[bash] reassigning referenced variables in functions

Hello all,

Problem.
----------
I'm trying to reassign a referenced variable passed to a 'local'
variable in a function but the local variable refuses to be assigned
the content of the referenced variable.

Any ideas would be appreciated.

Objective.
-----------
Eliminate all $VAR variable format and pass function parameters
as referenced variables, i.e. VAR, to standardise, simplify and
minimise soft errors. In addition, the function has to be self
contained for reuseability purposes.

As long as the calling of the function occurs with referenced
variables, reassignment to local variables within functions can
be passed by value.

Examples.
------------

passing variables by value
------------------------------
declare VAR1
declare VAR2=/path/to/file/or/directory
func_name VAR1 $VAR2
echo returned value from function VAR1 = $VAR1

function func_name
{
local FVAR1
local FVAR2=$2
FVAR1= # do something with FVAR2 and assign it to variable
eval "$1=$FVAR1"
}

This method works OK. However,

required methodology
-------------------------
# call function without using $VAR method
func_name VAR1 VAR2

# assign referenced variable VAR2 to local variable FVAR2
# using what would have seemed to be the logical method
# i.e. the reverse of assigning values to referenced variables
# as in the end of the function above
local FVAR2; eval "$FVAR2=$2"
# The above method seems to treat the expression as a command
# and the following error message is issued,
# =VAR2: command not found

# this assigns the name of the reference variable
local FVAR2; eval "FVAR2=$2"
# result: FVAR2=VAR2

Conclusion.
-------------
Using 'eval' to reassign referenced variables in reverse, in
contrast to assigning values to referenced variables,
doesn't work, i.e. eval "$1=$VAR" (OK), eval "$VAR=$1" (NOT OK)

Also, I tried using various combinations of brackets and a method
used to pass arrays by value (1) but still no success.

References:
-------------
1) Bash array passing-by-value - Thank Google!
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

reassigning key functions in super user mode

At my old job the TAB key was used in Super User mode rather than the default of the ESC key. I was wondering how to reassign the TAB key so that it acquires the functions of the ESC key. Thanks for the help. -Shana (5 Replies)
Discussion started by: lehcar511
5 Replies

2. Shell Programming and Scripting

Variables and functions

I have a script that ultimately will FTP data to certain directories based on a character in the filename. I am creating a function within my script to handle the FTP call. Are the variables that are setup in the original script accessible to the function? If not, is there a way to allow them to... (4 Replies)
Discussion started by: dfb500
4 Replies

3. Shell Programming and Scripting

awk - arithemetic functions with external variables

I'm trying to get awk to do arithmetic functions with external variables and I'm getting an error that I cannot figure out how to fix. Insight would be appreciated money=$1 rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table) rate2=$(awk -F"\t"... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

4. Programming

Trouble with Date Variables and Functions in PL/SQL

Hi, In the course of my script i have to compare SYSDATE with the 15th of the current month: if it is greater than i should set a variable date to 15th of the next month if less than i should set it to the 15th of the current month. In other words the question is how to set a date variable... (2 Replies)
Discussion started by: fmina
2 Replies

5. Shell Programming and Scripting

bash functions arguments

This script is called fuu; #!/bin/bash speak() { case $1 in 1)echo one ;; 2)echo two ;; 3)echo three ;; esac } speak exit 0 when i run fuu 2 i expect "two" like... (2 Replies)
Discussion started by: Tártaro
2 Replies

6. Shell Programming and Scripting

variables and functions

can somebody telll me what my values are not being displayed in the function func1() {oracle@im4s012nz1_DUMMY}$ cat x1.ksh #!/bin/ksh getpwd() ( set -x . /home/oracle/dba/bin/CyberArk/CyberArk_GetPass.ksh ORA_USER=$DB_UID ORA_PWD=$DB_PWD echo "Here I am 1... (6 Replies)
Discussion started by: BeefStu
6 Replies

7. Shell Programming and Scripting

functions and variables in bash

I have a bash script with some functions as below and am wondering if I can use the variables declared in setup in the other functions and in the rest of the bash script. setup(){ none=0; low=1; medium=2; high=3; debug=4 var1="red" var2="fred" } create_basemap() { ... (7 Replies)
Discussion started by: kristinu
7 Replies

8. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

9. Shell Programming and Scripting

Bash looking in different directory for file that isn't referenced in command

When I run the below bash I get the expected output, which is the sum of all matching targets less than 20 in $file1. The filename in the directory is fixed (in bold). for file1 in /home/cmccabe/Desktop/test/panel/reads/16-0000_EPIL70.txt ; do bname=`basename $file1` ... (3 Replies)
Discussion started by: cmccabe
3 Replies

10. Shell Programming and Scripting

Bash functions sequence ?

OK, I know function has to be defined first - in sequence - before it can be used. So the script has to be build "bottoms -up style, if you pardon my expression. I am running into a problem reusing function and breaking the sequence. It would be nice to be able to see the function... (10 Replies)
Discussion started by: annacreek
10 Replies
upvar(n)						       Tcl Built-In Commands							  upvar(n)

__________________________________________________________________________________________________________________________________________________

NAME
upvar - Create link to variable in a different stack frame SYNOPSIS
upvar ?level? otherVar myVar ?otherVar myVar ...? _________________________________________________________________ DESCRIPTION
This command arranges for one or more local variables in the current procedure to refer to variables in an enclosing procedure call or to global variables. Level may have any of the forms permitted for the uplevel command, and may be omitted if the first letter of the first otherVar isn't # or a digit (it defaults to 1). For each otherVar argument, upvar makes the variable by that name in the procedure frame given by level (or at global level, if level is #0) accessible in the current procedure by the name given in the corresponding myVar argu- ment. The variable named by otherVar need not exist at the time of the call; it will be created the first time myVar is referenced, just like an ordinary variable. There must not exist a variable by the name myVar at the time upvar is invoked. MyVar is always treated as the name of a variable, not an array element. Even if the name looks like an array element, such as a(b), a regular variable is created. Oth- erVar may refer to a scalar variable, an array, or an array element. Upvar returns an empty string. The upvar command simplifies the implementation of call-by-name procedure calling and also makes it easier to build new control constructs as Tcl procedures. For example, consider the following procedure: proc add2 name { upvar $name x set x [expr $x+2] } Add2 is invoked with an argument giving the name of a variable, and it adds two to the value of that variable. Although add2 could have been implemented using uplevel instead of upvar, upvar makes it simpler for add2 to access the variable in the caller's procedure frame. namespace eval is another way (besides procedure calls) that the Tcl naming context can change. It adds a call frame to the stack to rep- resent the namespace context. This means each namespace eval command counts as another call level for uplevel and upvar commands. For example, info level 1 will return a list describing a command that is either the outermost procedure call or the outermost namespace eval command. Also, uplevel #0 evaluates a script at top-level in the outermost namespace (the global namespace). If an upvar variable is unset (e.g. x in add2 above), the unset operation affects the variable it is linked to, not the upvar variable. | There is no way to unset an upvar variable except by exiting the procedure in which it is defined. However, it is possible to retarget an | upvar variable by executing another upvar command. | Traces and upvar | Upvar interacts with traces in a straightforward but possibly unexpected manner. If a variable trace is defined on otherVar, that trace | will be triggered by actions involving myVar. However, the trace procedure will be passed the name of myVar, rather than the name of oth- | erVar. Thus, the output of the following code will be localVar rather than originalVar: | proc traceproc { name index op } { | puts $name | } | proc setByUpvar { name value } { | upvar $name localVar | set localVar $value | } | set originalVar 1 | trace variable originalVar w traceproc | setByUpvar originalVar 2 | } | If otherVar refers to an element of an array, then variable traces set for the entire array will not be invoked when myVar is accessed (but | traces on the particular element will still be invoked). In particular, if the array is env, then changes made to myVar will not be passed | to subprocesses correctly. SEE ALSO
global(n), namespace(n), uplevel(n), variable(n) KEYWORDS
context, frame, global, level, namespace, procedure, variable Tcl upvar(n)
All times are GMT -4. The time now is 11:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy