我希望将NS3模拟的返回/退出代码作为Bash脚本中的一个变量来捕获。
码
#!/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 valueThe Issue
由于NS3仿真是使用waf (https://gitlab.com/ita1024/waf/)运行的,仿真的返回代码由waf处理,然后waf向bash脚本生成0的返回码值,因此不管仿真产生的退出代码如何,rv总是设置为零。
请求
如何将rv的值设置为NS3模拟的退出代码而不是waf执行的退出代码?
在通过C++模拟可以执行哪些NS3代码方面,我有很大的灵活性,包括控制返回代码值。
干杯
发布于 2021-08-31 08:59:04
问题是,由wscript编写的ns3不关心执行程序的返回代码。
您可以这样修改wscript:
# 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,我在这里帮不上忙。
发布于 2021-09-08 08:08:46
我通过使用文件将值从NS3传递到bash环境,从而解决了这个问题( NS3执行完成后将值从NS3传递给bash ),从而有效地绕过了waf。
下面是我的注释代码:
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++ (放置在NS3模拟中的代码,称为“模拟-名称”)
#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.https://stackoverflow.com/questions/68983258
复制相似问题