一定数量的输出后如何终止脚本 [英] How to Terminate Script after a certain number of output

查看:64
本文介绍了一定数量的输出后如何终止脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,它执行一些处理/记录工作,并将其写入文件.它会连续运行,直到用户终止它为止.我要做的是自动终止-连续运行此脚本,直到它将文件填充到用户指定的特定大小或行数为止.这可能吗?

I have a bash script that does some processing/logging stuff, and writes this to a file. It runs continuously until user terminates it. What I want to do is to auto termination- run this script continuously until it fills the file up to a certain size or number of lines specified by user. Is this possible?

我想不出该怎么做,因为如果此脚本是连续的,则无法使用条件语句.我只能考虑使用线程,但是我是一个初学者,不希望它这么复杂.

I can't think of how this can be done because i can't use conditionals if this script is continuous. I can only think of using threads, but i am a beginner at this, and don't want it to be so complex.

推荐答案

正如@ 123所说,您可以使用 head 将输出限制为给定的行数,例如

As @123 said, you can use head to limit the output to a given number of lines like

continuous_command | head -n 20 > output_file

将显示前20行,然后当 continuous_command 尝试打印其第21行时,它将失败,因为管道的另一端已退出,因此 continuous_command 将得到一个 SIGPIPE 信号,通常会导致程序终止.

would show the first 20 lines, then when continuous_command tried to print its 21st it would fail because the other end of the pipe has exited, so continuous_command would get a SIGPIPE signal, which usually causes a program to terminate.

您可以删除>如果要输出到终端而不是文件,请使用output_file 部分

这篇关于一定数量的输出后如何终止脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆