bash 在循环“~$ ./program >”时删除我的文件文件.dat” (gnuplot)

发布于 2024-12-23 02:08:07 字数 1150 浏览 0 评论 0原文

我正在 bash 上编写一些脚本,以使用 gnuplot 绘制数据。但这不起作用。

我有一个将数据发送到命令行的程序。 我将这些数据收集到一个文件中,然后循环此过程以获得所需的结果。假设我有这个脚本

 N = 10
 ./program.exe > data_$N.dat # Creating the data to be plotted
 for ((i = 0 ; i<1 ; i++)) do # Dummy loop to throw all together to gnuplot
  echo "set size square"
  echo "set xrange[-$N-1:$N+1]; set yrange[-$N-1:$N+1];"
  echo "plot '-' using 1:2"
  for((j = 0 ; j <= 9 ; j++)) do
   # Throwing the data to gnuplot
   echo "cat data_$N.dat" 
   # Updating the file and overwriting on it
   echo "cat data_$N.dat | xargs ./program.exe > data_$N.dat" | bash  
   echo "e" 
   echo "pause 0.5"
  done
 done | gnuplot -persist # Throwing the formatted data to gnuplot

,那么,基本上我将格式化文本从文件扔到 gnuplot,更改它的内容,但不更改文件名。

当然,我不想更改文件的名称,因为我使用该文件进行与该步骤相对应的计算,然后用更新的数据覆盖该文件。一种缓冲区。计算流程将是

./program.exe > data.dat # Making the initial data
begin gnuplot loop 
 throw data to gnuplot
 "cat data.dat | xargs ./program.exe > data.dat" # update data overwriting file
end gnuplot loop
pipe all the script to gnuplot

希望很清楚,你可以帮助我:)。谢谢!

PD。我是一名物理学家。

I'm doing some script on bash to plot data with gnuplot. But it doesn't work.

I have a program which throws data to the command line.
I collect that data on a file and then I loop this process over to get the desired result. Say I have this script

 N = 10
 ./program.exe > data_$N.dat # Creating the data to be plotted
 for ((i = 0 ; i<1 ; i++)) do # Dummy loop to throw all together to gnuplot
  echo "set size square"
  echo "set xrange[-$N-1:$N+1]; set yrange[-$N-1:$N+1];"
  echo "plot '-' using 1:2"
  for((j = 0 ; j <= 9 ; j++)) do
   # Throwing the data to gnuplot
   echo "cat data_$N.dat" 
   # Updating the file and overwriting on it
   echo "cat data_$N.dat | xargs ./program.exe > data_$N.dat" | bash  
   echo "e" 
   echo "pause 0.5"
  done
 done | gnuplot -persist # Throwing the formatted data to gnuplot

So, basically I throw formatted text to gnuplot from a file, changing the content of it, but without changing the name of the file.

Of course, I don't wanna change the name of the file because I make the calculations corresponding to that step with that file, and then I overwrite the file with the updated data. Kind of a buffer. The computational flow would be

./program.exe > data.dat # Making the initial data
begin gnuplot loop 
 throw data to gnuplot
 "cat data.dat | xargs ./program.exe > data.dat" # update data overwriting file
end gnuplot loop
pipe all the script to gnuplot

Hope is clear and you can help me :). Thanks!

PD. I'm a physicist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-12-30 02:08:07

> 的重定向是由 shell 完成的。因此,执行 cat data_$N.dat | 时发生的第一件事是xargs ./program.exe > data_$N.dat 是 shell 打开 data_$N.dat 进行写入并截断它。现在它是空的,所以当 cat 启动时它会找到一个文件,但它已经被截断了。

只需将您的输出重定向到临时位置(通常在返回 PID 的位置中使用 $$ ,以便您的脚本在某种程度上是独立的)。所以 ...> tmp_$$.dat 然后就可以mv -f tmp_$$.dat data_$N.dat

Redirection with > is done by the shell. So the first thing that happens when executing cat data_$N.dat | xargs ./program.exe > data_$N.dat is that the shell opens data_$N.dat for write and truncates it. Now it's empty, so when cat starts it finds a file but it has already been truncated.

Just redirect your output to a temporary location (it's common to use $$ in that which returns your PID so your scripts are somewhat independent). So ... > tmp_$$.dat and then right after that you can mv -f tmp_$$.dat data_$N.dat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文