[英]How to use output of C++ program as variables in a Bash script
I'm trying to come up with a work around to perform a few math functions for a script since bash apparently cannot do anything other than integer math (or can it even do that?). 我正在尝试为脚本执行一些数学函数,因为bash显然除了整数数学之外不能做任何事情(或者甚至可以做到这一点?)。
The script I'm coming up with needs to write a series of macros which will eventually be used for a simulation program. 我想出的脚本需要编写一系列宏,这些宏最终将用于模拟程序。 I'm currently just trying to output positions of a particle source which are used as parameters of the macro. 我目前正在尝试输出粒子源的位置,这些位置用作宏的参数。
The C++ Program I wrote is very simple, it takes in i and outputs x and y as such: 我写的C ++程序非常简单,它接受i并输出x和y这样:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double i;
double theta = 11 * (3.1415926535897932384626/180);
cin >> i;
double b = 24.370;
double y = (b/i)*sin(theta);
double x = (b/i)*cos(theta);
cout << x << " " << y << endl;
return 0;
}
The script I'm writing outputs a bunch of stuff that has to do with the macros i'm trying to create, but the line I'm stuck on (labled as (1) ) needs to do something like this... 我正在编写的脚本输出了一些与我正在尝试创建的宏有关的东西,但是我坚持使用的行(标记为(1))需要做这样的事情......
for i in {1..100}
do
echo "./a.out" #calling the C program
echo "$i" #entering i as input
x = first output of a.out, y = second output a.out #(1) Confused on how to do this part!
echo -n "/gps/position $x $y 0 27.7 cm" > mymacro.mac
done DONE
I know there has to be a really easy way to do this but I'm not exactly sure what to do. 我知道必须有一个非常简单的方法来做到这一点,但我不确定该怎么做。 I basically just need to use the output of ac program as variables in the script. 我基本上只需要使用ac程序的输出作为脚本中的变量。
You should probably consider passing $i
to your c++ program as a variable and using argv
to read it in ( this might be helpful). 您应该考虑将$i
作为变量传递给您的c ++程序并使用argv
读取它( 这可能会有所帮助)。 That's most likely the "proper" way to do it. 这很可能是“正确”的方式。 Then you can use this for bash: 然后你可以用它来做bash:
#!/bin/bash
IFS=' ';
for i in {1..100}
do
read -ra values <<< `./a.out $i`
x=${values[0]}
y=${values[1]}
echo -n "/gps/position $x $y 0 27.7 cm" > mymacro.mac
done
And are you sure you want > mymacro.mac
instead of >> mymacro.mac
(if the former is inside of a loop, only the last value will be written to the file mymacro.mac
. 你确定你想要> mymacro.mac
而不是>> mymacro.mac
(如果前者在循环内,只有最后一个值将被写入文件mymacro.mac
。
You can use cegfault's answer, or more simply: 您可以使用cegfault的答案,或者更简单:
read val1 val2 <<< $(./a.out $i)
Which executes a.out
and stores the two numbers in $val1
and $val2
. 执行a.out
并将两个数字存储在$val1
和$val2
。
You may find it even easier to use awk
, which does handle floating-point numbers and most math functions. 您可能会发现使用awk
更容易,它确实处理浮点数和大多数数学函数。 Here's an arbitrary example: 这是一个任意的例子:
bash> read x y <<< $(echo 5 | awk '{ printf "%f %f\n", cos($1), sin($1) }')
bash> echo $x
0.283662
bash> echo $y
-0.957824
If your script will be long-lived or has a lot of data to process, and especially since you're writing part of the functionality in a C++ program anyway, you'll probably do a lot better to just write the whole thing in C++, and the end result will be a whopping lot faster... and more centralized & easier to see what's going on. 如果您的脚本将是长期存在的或者需要处理大量数据,特别是因为您无论如何都要在C ++程序中编写部分功能,那么在C ++中编写完整的东西可能会好得多。 ,最终的结果将会快得多......而且更集中,更容易看到正在发生的事情。
#include <iostream>
#include <math.h>
#include <sstream>
#include <fstream>
int main()
{
const double theta = 11 * (3.1415926535897932384626/180);
const double b = 24.370;
for (int n=1; n<=100; ++n)
{
double i = n;
double y = (b/i)*sin(theta);
double x = (b/i)*cos(theta);
// Two ways of sending the output to mymacro.mac......
// 1. Via the system() call.....
std::stringstream ss;
ss << "echo -n \"/gps/position " << x << ' ' << y << " 0 27.7 cm\" > mymacro.mac";
system(ss.str().c_str());
// 2. Via C++ file I/O.....
std::ofstream ofile("mymacro.mac");
ofile << "/gps/position " << x << ' ' << y << " 0 27.7 cm";
ofile.close(); //...not technically necessary; ofile will close when it goes out of scope.
}
}
Note that this solution replicates your example with exquisite fidelity, including the part about overwriting the file 'mymacro.mac' on every loop iteration. 请注意,此解决方案以精确的保真度复制您的示例,包括在每次循环迭代时覆盖文件'mymacro.mac'的部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.