如何添加“手表” shell 脚本的命令?
我有一个脚本,用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意unix哲学:
应用于你的情况:
你的“讨厌的黑客”实际上是朝着正确方向迈出的一步。
你永远不会知道是否有一天你需要“观看——无标题”另一个剧本。那时,如果您遵循 Unix 哲学,您就已经有了一个脚本来做到这一点。
您已经看到了尝试让脚本同时做太多事情的一个复杂情况 - 引用地狱。
保持简单。
Heed the unix philosophy:
Applied to your case:
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.
正确的用法是:
the correct usage would be:
tail -f
有帮助吗?男人尾巴
Does
tail -f <file>
help?man tail