C++ 如何使用C++;在Bash脚本中作为变量编程
我正试图找到一个解决方案,为脚本执行一些数学函数,因为bash显然除了整数数学之外什么都做不了(或者它甚至可以做到?) 我要编写的脚本需要编写一系列宏,这些宏最终将用于模拟程序。我目前正在尝试输出粒子源的位置,这些位置用作宏的参数 < >我编写的C++程序非常简单,输入I,输出X和Y:C++ 如何使用C++;在Bash脚本中作为变量编程,c++,scripting,C++,Scripting,我正试图找到一个解决方案,为脚本执行一些数学函数,因为bash显然除了整数数学之外什么都做不了(或者它甚至可以做到?) 我要编写的脚本需要编写一系列宏,这些宏最终将用于模拟程序。我目前正在尝试输出粒子源的位置,这些位置用作宏的参数 < >我编写的C++程序非常简单,输入I,输出X和Y: #include <iostream> #include <math.h> using namespace std; int main() { double i; double
#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;
}
完成
我知道必须有一个非常简单的方法来做到这一点,但我不确定该怎么做。我基本上只需要使用c程序的输出作为脚本中的变量 > P>你可能应该考虑把<代码> $i/Cube >作为C++变量,使用<代码> ARGV 来读取它(可能会有帮助)。这很可能是“正确”的方法。然后您可以将其用于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
#/bin/bash
IFS=“”;
对于{1..100}中的i
做
read-ra值mymacro.mac
而不是>mymacro.mac
(如果前者在循环中,则只有最后一个值会写入文件mymacro.mac
。您可以使用cegfault的答案,或者更简单地说:
read val1 val2 <<< $(./a.out $i)
<>如果你的脚本将是长期的或有大量的数据要处理,尤其是因为你在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.
}
}
#包括
#包括
#包括
#包括
int main()
{
常数双θ=11*(3.1415926535897932384626/180);
常数双b=24.370;
对于(int n=1;n表示-a
:“从0开始,将单词分配给数组变量名的顺序索引。”这样,我们可以确定${values[0]}
是第一个数字,${values[1]}
是第二个数字。更重要的是,我在使用-ra
做其他事情,复制粘贴让我的生活更轻松;)哎呀-我的意思是-a
并编辑了我的评论。如果你更新你的答案,我会删除此评论!嗯,我收到一个错误,说“评论只能编辑5分钟”…对不起:(-r
用于确保反斜杠不会成为转义字符。在这个应用程序中,它可能没有意义(因为它永远不会出现),但我只是在我自己的东西中使用了-ra
,所以很容易复制粘贴,不用考虑它;)我喜欢这个;看起来比我的解决方案更优雅一点(一句台词让世界运转起来)那么,你的剧本成功了吗?下面的答案有用吗?
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
#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.
}
}