bnbfreak
bnbfreak

Reputation: 353

Change variable value in TCL file and re-run simulation using bash script

I have TCL file a.tcl from ns2 as follows :

set opt(x)              1000                                
set opt(y)              1000                                
set opt(z)              15                                  
set opt(per_11ac)       1                                   
set opt(nch)            4                                   
set opt(cbr_rate_DL)        0.05Mb                              
set opt(cbr_rate_UL)        10Mb

I want to change opt(cbr_rate_DL) value automatically from bash script hence for the next simulation I can change to 0.1Mb, 0.5Mb, and 10Mb and re-run the simulation with new value. How to do that in bash script?

Upvotes: 0

Views: 451

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137587

The best way of doing this is probably to pass the value in using an environment variable. In Tcl scripts, environment variables are mapped to the global env array, so you change your code to include use (with extra spaces and commentary for clarity):

# Set up a default, for safety
set opt(cbr_rate_DL)        0.05Mb

# Has the environment variable been set?
if {[info exists env(CBR_RATE_DL)]} {

    # It has! Copy to where it belongs
    set opt(cbr_rate_DL)    $env(CBR_RATE_DL)
}

Then you can run your script with another value there by just setting the CBR_RATE_DL environment variable to whatever you want. (Remember to export it from your shell if you're using a bourne-shell-derived environment, of course.)

Upvotes: 1

muralisc
muralisc

Reputation: 16

You can use a for loop construct to do this

for val in 0.1 0.5 10
do
   sed -i "/set opt(cbr_rate_DL)/ s/[0-9.]\+/$val/" a.tcl
   // run the simulation here
done

the sed script above modifies the a.tcl file inplace (-i) . It first search each line for /set opt(cbr_rate_DL)/. if line contains set opt(cbr_rate_DL) if so substitutes for number sequence with dots s/[0-9.]\+/$val/ with the value in $val

Upvotes: 1

Related Questions