[bash] reassigning referenced variables in functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] reassigning referenced variables in functions
# 1  
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!
# 2  
Old 04-12-2009
what do you know...

After several more attempts, I seem to have solved the problem. Might
not be the best solution but it seems to work. Essentially, it's a hack from
the link above that reassigns an array by passing values.

To assign a value from a referenced variable to another variable in a function,

for a string:
FVAR_STR=( $(eval "echo \$$1") )

and for an integer:
FVAR_INT=( $(eval "echo \$1") )

A.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question