Updating a variable within a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating a variable within a variable
# 8  
Old 03-21-2017
Quote:
Originally Posted by rovf
But it might be the case that your colleagues start saying nasty things to you if you are actually doing it in productive code ;-)
Very true! ;-)) eval is the command which sole purpose is to be avoided, so to say.

Quote:
Originally Posted by rovf
Code:
container()
{
   echo "abc${myvar}xyz"
}
myvar=4711
actual_value=$(container)

In a stricter sense this is also problematic. You need to know in the main program, that the variable in container() is named in a certain way (here "myvar"). You could alleviate this dependency by doing:

Code:
container()
{
   echo "abc${1}xyz"
}
myvar=4711
actual_value=$(container $myvar)

# 9  
Old 03-21-2017
Quote:
Originally Posted by bakunin
You need to know in the main program, that the variable in container() is named in a certain way (here "myvar"). You could alleviate this dependency by doing:

Code:
container()
{
   echo "abc${1}xyz"
}
myvar=4711
actual_value=$(container $myvar)

This could indeed be an improvement, or not, depending on the context.

If application logic tells us that this variable always MUST be the same (for example, if it is an environment variable with an application-wide well-defined meaning, such as USER or CLASSPATH or OUR_GIT_REPO, I think it is better not to pass it as parameter. Having always the same parameter is annoying to type and invites typing mistakes. If this context is used with different variables often (i.e. our function container works a bit like a "formatter" for values), your approach is better.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

error while updating rows in sql with values in array variable

Hi, I need to update rows in a table based on the values in an array variable. code is : while read line do error_msg="$(echo $line)" index=`expr $index+1` done <"logs/$ffile" rows_count=${#error_msg } i=0 while do echo "error msgs is... (2 Replies)
Discussion started by: RP09
2 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Updating variable

Hi, I have been attempting to install source code from xcrysden with little to no luck. I have attempted to follow the instructions given on the site but made the mistake on the code I downloaded. This was pointed out to my by the developer and I have since downloaded the proper code. However... (4 Replies)
Discussion started by: Zbay
4 Replies

5. Shell Programming and Scripting

awk - updating variable in if statement

I have another question I am stuck at :wall: I have a text file with two columns, like so... 2 0.0627279 3 0.0794451 4 0.108705 5 0.137739 6 0.190394 7 0.217407 8 0.241764 9 0.344458 10 0.460762 I'd like to go through the file line by line until the value in the second column... (3 Replies)
Discussion started by: origamisven
3 Replies

6. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

7. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

8. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

10. UNIX for Advanced & Expert Users

AutoSys (updating global variable)

Hello all, I am very new at both AutoSys and VBScript. I have looked at the manual for hours however I just can't figure it out, the following is the problem: NOTE: AutoSys is being used in all Windows environment. But realizing that this is a UNIX forum, please do provide the answer however... (1 Reply)
Discussion started by: mik357
1 Replies
Login or Register to Ask a Question