找不到字符串后如何在shell中停止? [英] how to stop while do in shell once a string is not found?

查看:68
本文介绍了找不到字符串后如何在shell中停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本:

#!/bin/bash
while [ true ]
do

    if tail -1 /tmp/test | grep 'line3'
    then
        echo found
        sleep 5
    else
        echo not found
    fi
done

每5秒钟查找一次 line3 .找不到 line3 后,如何使脚本停止?

It looks for line3 every 5 seconds. How do I make the script stop once line3 is not found ?

推荐答案

目前尚不清楚您为什么在 tail |中加入 while [true] 的原因.grep if,然后,else 作为 while 循环可以使用您的子句作为测试本身:

It is a little unclear why you are including while [ true ] around your tail | grep and if, then, else as the while loop can use your clause as the test itself:

#!/bin/bash

while tail -1 /tmp/test | grep 'line3'
do
    echo found
    sleep 5
done

echo "not found"

如果 包裹 if,then,else ,这没什么不对.

There is nothing wrong with wrapping if, then, else in while [ true ], it is just less than optimal.

这篇关于找不到字符串后如何在shell中停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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