如何添加“手表” shell 脚本的命令?

发布于 2024-11-30 15:43:12 字数 1121 浏览 1 评论 0原文

我有一个脚本,用于 grep 和 awks 并计算具有多个 http 状态代码的 IP 地址。不过,脚本是什么并不重要,因为我的目标如下:

我想在这个脚本中调用“watch”命令,以便它每隔几秒就用从动态 apache 日志接收到的新数据更新显示内容。

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

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

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

这是脚本:

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

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

有什么想法吗?

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

谢谢,

凯文

编辑:还有一件事,我可以在 true 时调用 ;执行<大量脚本>;睡觉1;清楚;但我讨厌那样。它有效,但屏幕闪烁,这不是执行此操作的正确方法。

编辑2:另一个有效的令人讨厌的黑客方法是简单地创建两个脚本。第一个是:

watch --no-title ./bad_request

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

编辑3(抱歉...):我把“watch”的-d标志去掉了。这对于我的脚本来说不是必需的。

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:

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.

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.

Here is the script:

#!/bin/bash
#identify repeated offenders through 401 and 403 status codes
  for ip in `awk '{print $1}' /var/log/apache2/* | sort -u`; do echo -n "$ip "; awk '{print $1 " " $9}' /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.

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.

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2024-12-07 15:43:12

注意unix哲学:

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

应用于你的情况:

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

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

你永远不会知道是否有一天你需要“观看——无标题”另一个剧本。那时,如果您遵循 Unix 哲学,您就已经有了一个脚本来做到这一点。

您已经看到了尝试让脚本同时做太多事情的一个复杂情况 - 引用地狱。

保持简单。

Heed the unix philosophy:

 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.

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

Keep it simple.

墨落画卷 2024-12-07 15:43:12

正确的用法是:

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

the correct usage would be:

watch -d --no-title "/bin/bash for ip in `awk '{print $1}' /var/log/apache2/* | sort -u`; do echo -n "$ip "; awk '{print $1 " " $9}' /var/log/apache2/* | egrep '( 401| 403)'| grep -c $ip; done | sort -k2 -r"
甜`诱少女 2024-12-07 15:43:12

tail -f 有帮助吗?

男人尾巴

Does tail -f <file> help?

man tail

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文