首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将NS3返回/退出代码(通过waf)捕获为Bash脚本中的变量

将NS3返回/退出代码(通过waf)捕获为Bash脚本中的变量
EN

Stack Overflow用户
提问于 2021-08-30 11:19:38
回答 2查看 64关注 0票数 1

我希望将NS3模拟的返回/退出代码作为Bash脚本中的一个变量来捕获。

代码语言:javascript
复制
#!/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

由于NS3仿真是使用waf (https://gitlab.com/ita1024/waf/)运行的,仿真的返回代码由waf处理,然后waf向bash脚本生成0的返回码值,因此不管仿真产生的退出代码如何,rv总是设置为零。

请求

如何将rv的值设置为NS3模拟的退出代码而不是waf执行的退出代码?

在通过C++模拟可以执行哪些NS3代码方面,我有很大的灵活性,包括控制返回代码值。

干杯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-31 08:59:04

问题是,由wscript编写的ns3不关心执行程序的返回代码。

您可以这样修改wscript

代码语言:javascript
复制
# 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)

并且/或您可以解析模拟的输出以获得所需的任何值(通常是在shell中获得结果的最佳方法,返回值仅用于错误处理)。我不认识ns3,我在这里帮不上忙。

票数 0
EN

Stack Overflow用户

发布于 2021-09-08 08:08:46

我通过使用文件将值从NS3传递到bash环境,从而解决了这个问题( NS3执行完成后将值从NS3传递给bash ),从而有效地绕过了waf

下面是我的注释代码:

Bash

代码语言:javascript
复制
#!/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++ (放置在NS3模拟中的代码,称为“模拟-名称”)

代码语言:javascript
复制
#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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68983258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档