简体   繁体   English

文件中的第一行并不总是以bash脚本打印

[英]First line in file is not always printed in bash script

I have a bash script that prints a line of text into a file, and then calls a second script that prints some more data into the same file. 我有一个bash脚本,它将一行文本打印到一个文件中,然后调用第二个脚本,将另外一些数据打印到同一个文件中。 Lets call them script1.sh and script2.sh. 让我们称它们为script1.sh和script2.sh。 The reason it's split into two scripts, is because I have different versions of script2.sh. 它被分成两个脚本的原因是因为我有不同版本的script2.sh。

script1.sh: script1.sh:

rm -f output.txt
echo "some text here" > output.txt
source script2.sh

script2.sh: script2.sh:

./read_time >> output.txt
./run_program
./read_time >> output.txt

Variations on the three lines in script2.sh are repeated. 重复了script2.sh中三行的变化。

This seems to work most of the time, but every once in a while the file output.txt does not contain the line "some text here". 这似乎在大多数情况下都有效,但每隔一段时间文件output.txt就不包含“some here here”这一行。 At first I thought it was because I was calling script2.sh like this: ./script2.sh . 起初我还以为是因为我在呼唤这样script2.sh: ./script2.sh But even using source the problem still occurs. 但即使使用source ,问题仍然存在。 The problem is not reproducible, so even when I try to change something I don't know if it's actually fixed. 问题是不可重复的,所以即使我试图改变一些东西,我也不知道它是否真的被修复了。

What could be causing this? 可能是什么导致了这个?

Edit: The scripts are very simple. 编辑:脚本非常简单。 script1 is exactly as you see here, but with different file names. script1与您在此处看到的完全相同,但文件名不同。 script 2 is what I posted, but then the same 3 lines repeated, and ./run_program can have different arguments. 脚本2是我发布的,但是重复相同的3行,而./run_program可以有不同的参数。 I did a grep for the output file, and for > but it doesn't show up anywhere unexpected. 我为输出文件做了一个grep,对于>但它没有出现意外的任何地方。

The way these scripts are used is that script1 is created by a program (the only difference between the versions is the source script2.sh line. This script1.sh is then run on a different computer (linux on an FPGA actually) using ssh. Before that is done, the output file is also deleted using ssh. I don't know why, but I didn't write all of this. Also, I've checked the code running on the host. The only mention of the output file is when it is deleted using ssh, and when it is copied back to the host after the script1 is done. 这些脚本的使用方式是script1由程序创建(版本之间的唯一区别是source script2.sh行。然后使用ssh在另一台计算机(实际上是FPGA上的linux)上运行此script1.sh)。在此之前,输出文件也使用ssh删除。我不知道为什么,但我没有写完所有这些。另外,我已经检查了主机上运行的代码。唯一提到的输出file是在使用ssh删除它时,以及在script1完成后将其复制回主机的时间。

Edit 2: I finally managed to make the problem reproducible at a reasonable rate by stripping script2.sh of everything but a single line printing into the file. 编辑2:我终于设法通过剥除脚本打印到文件中的所有内容,然后以合理的速率重现问题。 This also let me do the testing a bit faster. 这也让我更快地进行测试。 Once I had this I got the problem between 1 and 4 times for every 10 runs. 有了这个,我每10次运行就会遇到1到4次问题。 Removing the command that was deleting the file over ssh before the script was run seems to have solved the problem. 在脚本运行之前删除通过ssh删除文件的命令似乎已经解决了问题。 I will test it some more to be sure, but I think it's solved. 我会更多地测试它,但我认为它已经解决了。 Although I'm still not sure why it would be a problem. 虽然我还不确定为什么会出问题。 I thought that the ssh command would not exit before all the remove commands were executed. 我认为在执行所有删除命令之前,ssh命令不会退出。

It is hard to tell without seeing the real code. 没有看到真正的代码就很难分辨。 Most likely explanation is that you have a typo, > instead of >> , somewhere in one of the script2.sh files. 最有可能的解释是,你有一个错字, >而不是>>在script2.sh文件之一,某处。

To verify this, set noclobber option with set -o noclobber . 要验证这一点,请使用set -o noclobber设置noclobber选项。 The shell will then terminate when trying to write to existing file with > . 然后,当尝试使用>写入现有文件时,shell将终止。

Another possibility, is that the file is removed under certain rare conditions. 另一种可能性是,在某些罕见的情况下删除该文件。 Or it is damaged by some command which can have random access to it - look for commands using this file without >> . 或者它被一些可以随机访问它的命令损坏 - 在没有>>情况下使用这个文件查找命令。 Or it is used by some command both as input and output which step on each other - look for the file used with < . 或者某些命令使用它作为输入和输出相互踩 - 查找与<使用的文件。

Lastly, you can have a racing condition with a command outputting to the file in background, started before that echo . 最后,您可以使用命令输出到后台文件,在该echo之前启动。

Can you grep all your scripts for 'output.txt'? 你可以为'output.txt'grep所有脚本吗? What about scripts called inside read_time and run_program? 在read_time和run_program中调用的脚本怎么样?

It looks like something in one of the script2.sh scripts must be either overwriting, truncating or doing a substitution on output.txt. 看起来其中一个script2.sh脚本中的某些东西必须覆盖,截断或在output.txt上进行替换。

For example,there could be a '> output.txt' burried inside a conditional for a condition that rarely obtains. 例如,对于很少获得的条件,可能会在条件内部隐藏“> output.txt”。 Just a guess, but it would explain why you don't always see it. 只是一个猜测,但它可以解释为什么你不总是看到它。

This is an interesting problem. 这是一个有趣的问题。 Please post the solution when you find it! 请在找到解决方案后发布解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM