Updating a variable within a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating a variable within a variable
# 1  
Old 03-20-2017
Updating a variable within a variable

I am trying to increment a counter variable that sits within a seperate text variable. I am initializing the counter variable once only at the start of my code. I expected to be able to increment the counter and have the text variable (that countains the counter) display as incremented throughout my code, only this isn't the case.

For example, using a text file containing the word "hello" as it
s contents. I expected the code below to give me 2 seperate text files like so:

Code:
run1.txt "testing1"
run2.txt "testing2"

The issue is, I'm receiving:

Code:
run1.txt "testing1"
run2.txt "testing1"


Code:
!/bin/bash

run_number=0

((run_number++)) #run_number is now 1

run="testing$run_number" #run is now 'testing1'

echo $run_number #displays 1

cp template.txt run1.txt #make a copy of a file that contains "hello"

sed -i 's#hello#'$run'#g' run1.txt #replace "hello" with var run ("testing1")

cat run1.txt #display new contents of file, "testing1"

echo "--------------------------------------------"

((run_number++)) #increment variable so that now, run_number=2

echo $run_number #displays 2

cp template.txt run2.txt #make copy of file that contains "hello"

sed -i 's#hello#'$run'#g' run2.txt #replace "hello" with "testing2"

cat run2.txt #display "testing2"?? this returns testing1

I am aware that I can simply initialize the variable..

Code:
run="testing$run_number" #run is now 'testing1

later on, after incrementing "run_number" but this seems redundant.

Any help is appreciated.

F

Last edited by Scrutinizer; 03-20-2017 at 02:07 AM.. Reason: Additional code tags
# 2  
Old 03-20-2017
What you call "sitting within" is never the case.
After the assignment:
Code:
run="testing$run_number"

The variable run simply contains the string testing1
There is no variable contained in another variable..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-20-2017
I see. Do you know if there is a way to increment a variable within a variable and have it remain throughout? I don't want it to be a one time thing because I plan on incrementing it in a for loop over several iterations.
# 4  
Old 03-20-2017
Hi,

Not really, no. In Bash, variables are only ever updated at the time something is assigned to them. If other variables that were used to determine their value are themselves updated, this will not change the values of other variables dynamically.

A quick example is probably the easiest way to demonstrate this.

Code:
#!/bin/bash

number=1
string="Testing"

var=$string$number
echo "number: $number"
echo "string: $string"
echo "var: $var"
echo "---"

number=2
echo "number: $number"
echo "string: $string"
echo "var: $var"
echo "---"

var=$string$number
echo "number: $number"
echo "string: $string"
echo "var: $var"
echo "---"

Here's what you'll see when you run this.

Code:
$ ./script.sh 
number: 1
string: Testing
var: Testing1
---
number: 2
string: Testing
var: Testing1
---
number: 2
string: Testing
var: Testing2
---
$

So basically, the contents of a variable will only ever be updated when something is assigned to that variable, regardless of what happens to any other variables that were originally used to determine the contents of the variable in question.

Hope this helps clear things up.
# 5  
Old 03-20-2017
Quote:
Originally Posted by Flip-Flop
I see. Do you know if there is a way to increment a variable within a variable and have it remain throughout? I don't want it to be a one time thing because I plan on incrementing it in a for loop over several iterations.
I think you really have to rebuild the run variable every time you increment the integer variable.
Examples:
Code:
((run_number++))
run=running$run_number
# or
run=running$((++run_number))

Andrew
# 6  
Old 03-20-2017
Or try a loop

Code:
#!/bin/bash
for ((run_number=1; run_number<=2; run_number++))
do
  run=testing$run_number
  runfile=run${run_number}.txt
  cp "template.txt" "$runfile"
  echo "$run_number"
  sed -i "s#hello#${run}#g" "$runfile" #replace "hello" with var run
  cat "$runfile" #display new contents of file 
  echo "--------------------------------------------"
done

# 7  
Old 03-21-2017
Quote:
Originally Posted by Flip-Flop
I see. Do you know if there is a way to increment a variable within a variable and have it remain throughout?
Technically no. You can, of course, play dirty tricks:

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

But it might be the case that your colleagues start saying nasty things to you if you are actually doing it in productive code ;-)

Another possibility is to use a function, which bypasses the eval:

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

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