在Shell脚本中进行文件写入缓冲(File write buffering in Shell script)

编程入门 行业动态 更新时间:2024-10-09 18:20:37
在Shell脚本中进行文件写入缓冲(File write buffering in Shell script)

我有一个启动的shell脚本,它从文件中读取一个值,将其加1并写回。 之后,我进行系统的电源循环(关闭并打开电源)。 我试图用这种方法记录重新启动的次数。 但是我发现文件计数器始终保持为1.如果使用reboot命令执行重新引导,则文件中的计数器会正确递增。 这是因为文件写入是由内核缓冲和延迟的。 有没有办法强制它立即写入?

rc.user文件如下所示:

cd /root bash bootcounter.sh sleep 1

bootcounter.sh如下

rebootcount=$(<bootcount) rebootcount=$(($rebootcount+1)) echo $rebootcount >bootcount

谢谢...

I have a start up shell script which reads a value from the file, increments it by 1 and writes it back. After that I do power-cycling of the system(switch off and switch on the power supply). I am trying to record the number of reboots using this way. But I find that the file counter always remains at 1. If I do the reboot using the reboot command, the counter in the file increments properly. Is this because the file write is buffered and delayed by the kernel. Is there a way to force it to write immediately?

The rc.user file is as follows:

cd /root bash bootcounter.sh sleep 1

bootcounter.sh is as follows

rebootcount=$(<bootcount) rebootcount=$(($rebootcount+1)) echo $rebootcount >bootcount

Thanks...

最满意答案

你想要同步命令。 这应该刷新所有的文件系统。

count=$( cat bootcount ) echo $( expr $count + 1 ) > bootcount sync

不过,您应该使用完整路径来引导帐户。

You want the sync command. This should flush all the file systems.

count=$( cat bootcount ) echo $( expr $count + 1 ) > bootcount sync

You should probably use a full path to bootcount, though.

更多推荐

本文发布于:2023-08-07 13:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   文件   Shell   File   script

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!