1

I would like to capture the return/exit code of my NS3 simulation as a variable in my Bash script.

Code

#!/bin/bash

rv=-1    #set initial return value
./waf --run "ns3-simulation-name --simulationInput1=value1 --simInput2=value2"    #run ns3 simulation using waf
rv=$?    #capture return value
echo -e "return value captured from program rv=$rv\n"    #output return value

The Issue

Since the NS3 simulation is run using waf (https://gitlab.com/ita1024/waf/), the return code of the simulation is processed by waf, which then produces a return code value of 0 to the bash script hence rv is always set to zero irrespective of the exit code produced by the simulation.


Request

How can the value of rv be set to the exit code of the NS3 simulation and not that of the waf execution?

I have much flexibility in what C++ code can be executed by the NS3 simulation, including controlling the return code value.

Cheers

2 Answers2

0

The problem is that the wscript written by ns3 doesn't care of the return code of the executed program.

You can either modify the wscript like this:

# near line 1426 on current repository
if Options.options.run:
    rv = wutils.run_program(
        Options.options.run,
        env, 
        wutils.get_command_template(env),                    
        visualize=Options.options.visualize,
   )
   raise SystemExit(rv)

And/or you can parse the output of the simulation to get whatever value you want (usually the best way to get results in shell, the return value is only for error handling). I don't know ns3, I can't help here.

neuro
  • 14,948
  • 3
  • 36
  • 59
0

I solved this problem (of passing a value from NS3 to bash upon completion of NS3 execution) by using a file to transfer values from NS3 to the bash environment, effectively bypassing waf.

Below is my commented code:

Bash

#!/bin/bash
vfn=filename.var   # variable file name
touch $vfn   #create empty file

# Declare and set shell environment variables
export VAR1=-1 
export VAR2=0
export VAR3=1

# Load variables into file called $vfn
for var in VAR1 VAR2 VAR3
    do
        echo "$var="'"'"$(eval echo '$'"$var")"'"'
    done >> $vfn

# Run simulation passing filename $vfn into simulation.
./waf --run "simulation-name --input1=$vfn"    # Custom C++ code in the simulation uses --input1 to update the variable values in the specified file.

# Load updated values from file to the shell environment (variables)
. $vfn 

# --> Use updated values here...

# Delete variables file, as needed.
rm $vfn 

C++ (code placed within the NS3 simulation called "simulation-name" above)

#include <fstream>

// create a filestream object from the name of the file where the variables are stored. (variableFilename equals $vfn)
std::ofstream ofs(variableFilename, std::ofstream::trunc);

// Overwrite existing file with updated variable values using the required format.  
ofs << "VAR1=" << 3 << "\n";
ofs << "VAR2=" << 5 << "\n";
ofs << "VAR3=" << 7 << "\n";

ofs.close();   // close filestream object.
Dharman
  • 30,962
  • 25
  • 85
  • 135