LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2011, 02:07 PM   #1
nesrin
LQ Newbie
 
Registered: Nov 2010
Posts: 15

Rep: Reputation: 0
set a variable to a combination of two other variables


Hello,

I want to combine two variables and create a third one in bash script. But having a problem. the code looks like:

Quote:
a=0
b=0
c=$a"_"$b

echo $c
#the result is 0_0

let a="$a+1"
echo $a
#the result is 1

echo $c
#the result is 0_0 but I want to have 0_1
the problem is that variable c is not changing although variables a and b are changed. Any idea what would be the correct syntax to get that?

I would be grateful for some help.
thanks.
 
Old 07-27-2011, 02:19 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

I think you want something like this:
Code:
a=0
b=0
c='${a}_${b}'
eval echo "${c}"
0_0
a=1
eval echo "${c}"
1_0
You have to use 'eval' to get the desired results.
 
Old 07-27-2011, 02:21 PM   #3
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
When you assign variable c, the shell expands the values of a and b into c. If the value of a is changed later on the script, the variable c will have the previous value of a.

You'll have to perform the arithmetic on a and b first, then assign them to c:

Code:
a=0
b=0

a=$((a+1)) # a is 1
b=$((b+2)) # b is 2
c="${a}_${b}" # c is 1_2
 
Old 07-28-2011, 09:23 AM   #4
nesrin
LQ Newbie
 
Registered: Nov 2010
Posts: 15

Original Poster
Rep: Reputation: 0
thank you very much for your reply.
@Diantre: I have quite a long script and a & b are updated very often, that is why I want to define c once at the beginning. But worst case I will follow that idea.
@crts:eval works well in this example, but I actually have a little more complicated script. Since I thought the problem must be about how I define the variable c (not about how I call the variable c), I did not include in previous post my 4. variable var, which is defined with awk function.


Code:
a=0
b=0
c='${a}_${b}'
let b="$b+1"

var=$(awk -v c=${c} '$3 == c{print $2}' textfile)
echo $var
#in this case I dont get the correct entry on the textfile
Is it possible to get an updated version of variable c in awk function?
 
Old 07-28-2011, 09:31 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
c='${a}_${b}' sets c to the literal ${a}_${b}. The variables would be substituted if the were within double quotes: c="${a}_${b}"
 
Old 07-28-2011, 09:37 AM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Well,

I do not know what exactly you are trying to achieve but maybe you could do it all in awk? This would eliminate such ugly constructs like:
Code:
var=$(awk -v c=$(eval echo ${c}) '$3 == c{print $2}' file) # not really recommended; there is probably a better way
Maybe you can provide a bit more insight about your goal.
 
Old 07-28-2011, 09:39 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by catkin View Post
c='${a}_${b}' sets c to the literal ${a}_${b}. The variables would be substituted if the were within double quotes: c="${a}_${b}"
Hi catkin,

that is exactly not what the OP wants. He wants to "emulate" a pointer to $a and $b, so that $c will reflect changes of either one of them dynamically.
 
Old 07-28-2011, 09:44 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by crts View Post
Hi catkin,

that is exactly not what the OP wants. He wants to "emulate" a pointer to $a and $b, so that $c will reflect changes of either one of them dynamically.
Thanks for correcting my lazy reading of the thread crts
 
Old 07-28-2011, 04:45 PM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Shells do not support deferred evaluation, as far as I know; there is no way to define a variable that refers to other variables, with the references resolved at use time (and not at definition time).

I do know of two mechanisms you can use to emulate such variables, though. Assuming you use Bash or a POSIX shell (dash, for example):
  1. Use a function that outputs the desired expression.
    Instead of $name or ${name} you have to use $(name) (or `name`.
    Code:
        c () {
            echo -n "${a}_${b}"
        }
    
        # Use $(c) or `c` instead of ${c} or $c.
  2. Define a helper variable as the assignment expression. Evaluate the helper variable whenever the components may have changed, before using the assigned variable.
    While you can use the variable normally, you'll need to eval "$Update" (if Update contained the helper assignment expression) before using the variable in an expression, to make sure all changes have propagated properly:
    Code:
        Update='c="${a}_${b}"'
    
        # Run
        #    eval "$Update"
        # before using $c or ${c} to make sure all
        # changes in a and b are reflected in c.

It is often easier to modify the code that modifies the constituent variables, and add a "side effect" that updates the derivative variables. This is also surprisingly efficient, only adding a function call overhead to each assignment:
Code:
    EvalC='C="${A}_${B}"'

    SetAB () {
        A="$1"
        B="$2"
        eval "$EvalC"
        export A B C
    }

    SetA () {
        A="$*"
        eval "$EvalC"
        export A B C
    }

    SetB () {
        B="$*"
        eval "$EvalC"
        export A B C
    }

    # To set A to "Foo Bar" and B to "Alice-Bob", use
    #     SetAB "Foo Bar" "Alice-Bob"
    # or
    #     SetA "Foo Bar"
    #     SetB "Alice-Bob"
    # and C will always reflect the changes automatically.
There are obviously many variants of the above, and a number of tweaks you can do to make any of the above schemes better fit your needs.
 
Old 08-16-2011, 08:02 PM   #10
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Why not use an array ie
Code:
array=( 0 0 )
echo ${array[0]}_${array[1]}
0_0
((array[1]=array[1]+1))
echo ${array[0]}_${array[1]}
0_1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ps | grep command combination - Strange Output. Why Environment Variables Listed ? nkataria Linux - General 1 04-07-2010 03:50 AM
[SOLVED] Bash: Making a Variable out of two other variables. youarefunny Programming 4 03-29-2010 06:56 PM
How to make 2 variables from one variable value in awk intikhabalam Linux - General 1 07-30-2008 04:32 AM
Bind Mouse Button Combination to Keyboard Key Combination? neoAKiRAz Linux - Desktop 0 05-04-2007 12:49 PM
Cannot set LD_LIBRARY_PATH in .cshrc (able to set other env variables) senthilpr_in Linux - Newbie 4 02-26-2007 12:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration