bash 如何将“watch”命令添加到 shell 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7128554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:37:35  来源:igfitidea点击:

How do I add `watch' command to a shell script?

bashfunctionwatch

提问by Kevin

I have a script that greps and awks and counts for ip addresses with multiple http status codes. It doesn't really matter what the script is though, because my goal is as follows:

我有一个脚本,可以使用多个 http 状态代码对 ip 地址进行 grep 和 awks 计数。不过,脚本是什么并不重要,因为我的目标如下:

I want to invoke the `watch' command inside this script so that it will update the display every few seconds with new data received from the dynamic apache logs.

我想在这个脚本中调用‘watch’命令,这样它就会每隔几秒用从动态 apache 日志接收到的新数据更新显示。

i can call my script fine as follows:

我可以按如下方式调用我的脚本:

$ watch --no-title ./bad_request.sh

But I would much rather have the `watch' command inside the script itself, to make calling the script from the command prompt much cleaner.

但是我更愿意在脚本本身中使用“watch”命令,以使从命令提示符调用脚本更加清晰。

Here is the script:

这是脚本:

#!/bin/bash
#identify repeated offenders through 401 and 403 status codes
  for ip in `awk '{print }' /var/log/apache2/* | sort -u`; do echo -n "$ip "; awk '{print  " " }' /var/log/apache2/* | egrep "( 401| 403)"| grep -c $ip; done | sort -k2 -r

Now, I have tried just inserting "watch -d --no-title" inside the script, right before the for loop, but it errors out angrily. I think it's because it is only processing until it reaches the end of the first command. I've tried putting backticks, and $() around the entire script, as well. I've also tried making the bulk of the script a bash function, and calling watch on the function. No dice.

现在,我尝试在脚本中插入“watch -d --no-title”,就在 for 循环之前,但它愤怒地出错了。我认为这是因为它只在到达第一个命令的末尾时才进行处理。我也试过在整个脚本周围加上反引号和 $() 。我还尝试将大部分脚本设为 bash 函数,并在该函数上调用 watch。没有骰子。

Any ideas?

有任何想法吗?

By the way, I'm also open to improvements on the script - I do realize it's a bit redundant/inefficient. Of course, that should probably be reserved for a different Stack Overflow question.

顺便说一句,我也愿意改进脚本 - 我确实意识到它有点多余/效率低下。当然,这可能应该保留给不同的 Stack Overflow 问题。

Thanks,

谢谢,

Kevin

凯文

EDIT: And one more thing, I can just call while true; do <bulk of script>; sleep 1; clear;but I hate that. It works, but the screen flickers, and it's just not the right way to do this.

编辑:还有一件事,我可以打电话,while true; do <bulk of script>; sleep 1; clear;但我讨厌那样。它可以工作,但屏幕闪烁,这不是正确的方法。

EDIT 2: Another nasty hack that works, is to simply create two scripts. The first one is:

编辑 2:另一个讨厌的 hack 是简单地创建两个脚本。第一个是:

watch --no-title ./bad_request

And then just call that script. That works fine, but there has to be a better way.

然后只需调用该脚本。这很好用,但必须有更好的方法。

EDIT 3 (sorry...): I took the -d flag off of `watch'. It's not necessary for my script.

编辑 3(抱歉...):我从“watch”中去掉了 -d 标志。我的脚本不需要。

采纳答案by holygeek

Heed the unix philosophy:

注意 Unix 哲学:

 A program should do one and only one thing and do it well.

Applied to your case:

适用于您的案例:

A script should do one and only one thing and do it well.

Your "nasty hack" is actually a step in the right direction.

你的“讨厌的黑客”实际上是朝着正确方向迈出的一步。

You'll never know if one day you'll need to "watch --no-title" on another script. At that point if you follow the unix philosophy you'd already have a script to do that.

您永远不会知道有一天您是否需要在另一个脚本上“观看 --no-title”。到那时,如果您遵循 unix 哲学,那么您已经有一个脚本可以做到这一点。

You already see one complication of trying to make a script do too many things at once - quote hell.

您已经看到尝试让脚本一次做太多事情的一种复杂性 - 引用地狱。

Keep it simple.

把事情简单化。

回答by Thomas Berger

the correct usage would be:

正确的用法是:

watch -d --no-title "/bin/bash for ip in `awk '{print }' /var/log/apache2/* | sort -u`; do echo -n "$ip "; awk '{print  " " }' /var/log/apache2/* | egrep '( 401| 403)'| grep -c $ip; done | sort -k2 -r"

回答by mcandre

Does tail -f <file>help?

tail -f <file>帮助吗?

man tail

人尾