Register
It is currently Thu Apr 24, 2014 1:23 pm

double variable expansion


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Jul 05, 2010 7:46 pm   

Joined: Mon Jul 05, 2010 7:32 pm
Posts: 2
Hello,
I'm trying to write a bash script that will generate some code to be used in a scripting language (NEURON). I'm hoping to define my variables in the bash script, and then "transfer" these variables to my newly generated code.

For example, say I wanted to insert the variable IClamp_amp into my code, I have written the following bash script to do such a thing:

Code:
function wp()
{
echo $1 = $$1 > nrn_parameters.hoc   
echo $1 = $$1
}

export IClamp_amp=0.1
wp IClamp_amp


Based on this code, I would expect the line IClamp_amp = 0.1 to appear in nrn_parameters.hoc; however, I instead see the following:
IClamp_amp = 217891

(Note that I was able to get this working by replacing $$1 with $[$1]. However, this produces an error when IClamp_amp is a non-integer, and I need IClamp_amp to be a decimal number)

Thanks for looking! :)


Top
 Profile  
 PostPosted: Mon Jul 05, 2010 8:37 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

what you get is the PID of the current shell: $$1 is PID and 1
Code:
function wp() { echo "$FUNCNAME result: $1 = ${!1}"; }
IClamp_amp=0.1
wp IClamp_amp
wp result: IClamp_amp = 0.1
note that
$1 is the first parameter you're passing to the function
${!1} is indirection of $1


Top
 Profile  
 PostPosted: Mon Jul 05, 2010 10:57 pm   

Joined: Mon Jul 05, 2010 7:32 pm
Posts: 2
Thanks, I'm much obliged for your help. I'm very new at Bash, and have just read about indirection. These two links were useful:

http://www.enricozini.org/2008/tips/bash-indirection/
http://www.network-theory.co.uk/docs/ba ... nsion.html

Cheers,


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP