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
pthread_cond_init(3C)					   Standard C Library Functions 				     pthread_cond_init(3C)

NAME
pthread_cond_init, pthread_cond_destroy - initialize or destroy condition variables SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr); int pthread_cond_destroy(pthread_cond_t *cond pthread_cond_t cond= PTHREAD_COND_INITIALIZER; DESCRIPTION
The function pthread_cond_init() initializes the condition variable referenced by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes are used; the effect is the same as passing the address of a default condition variable attributes object. See pthread_condattr_init(3C). Upon successful initialization, the state of the condition variable becomes initialized. Attempting to initialize an already initialized condition variable results in undefined behavior. The function pthread_cond_destroy() destroys the given condition variable specified by cond; the object becomes, in effect, uninitialized. An implementation may cause pthread_cond_destroy() to set the object referenced by cond to an invalid value. A destroyed condition variable object can be re-initialized using pthread_cond_init(); the results of otherwise referencing the object after it has been destroyed are undefined. It is safe to destroy an initialized condition variable upon which no threads are currently blocked. Attempting to destroy a condition variable upon which other threads are currently blocked results in undefined behavior. In cases where default condition variable attributes are appropriate, the macro PTHREAD_COND_INITIALIZER can be used to initialize condi- tion variables that are statically allocated. The effect is equivalent to dynamic initialization by a call to pthread_cond_init() with parameter attr specified as NULL, except that no error checks are performed. RETURN VALUES
If successful, the pthread_cond_init() and pthread_cond_destroy() functions return 0. Otherwise, an error number is returned to indicate the error. The EBUSY and EINVAL error checks, if implemented, act as if they were performed immediately at the beginning of processing for the function and caused an error return prior to modifying the state of the condition variable specified by cond. ERRORS
The pthread_cond_init() function will fail if: EAGAIN The system lacked the necessary resources (other than memory) to initialize another condition variable. ENOMEM Insufficient memory exists to initialize the condition variable. The pthread_cond_init() function may fail if: EBUSY The implementation has detected an attempt to re-initialize the object referenced by cond, a previously initialized, but not yet destroyed, condition variable. EINVAL The value specified by attr is invalid. The pthread_cond_destroy() function may fail if: EBUSY The implementation has detected an attempt to destroy the object referenced by cond while it is referenced (for example, while being used in a pthread_cond_wait() or pthread_cond_timedwait()) by another thread. EINVAL The value specified by cond is invalid. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
pthread_cond_signal(3C), pthread_cond_broadcast(3C), pthread_cond_wait(3C), pthread_cond_timedwait(3C), pthread_condattr_init(3C), attributes(5), condition(5), standards(5) SunOS 5.11 23 Mar 2005 pthread_cond_init(3C)
All times are GMT -4. The time now is 11:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy