Search code examples
c++bashubuntuns-3waf

Capturing NS3 return/exit code (through waf) as a variable in Bash script


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


Solution

  • 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.