Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6364313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:08:53+00:00 2026-05-25T00:08:53+00:00

I have a script that greps and awks and counts for ip addresses with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:08:54+00:00Added an answer on May 25, 2026 at 12:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a bash script I'm writing that greps for a filepath. I want
I have a script that uses escape codes to highlight text matches in its
I have a small bash script that greps/awk paragraph by using a keyword. But
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax
I have a script that takes a table name and generates a control file
I have a script that renders graphs in gnuplot. The graphs all end up
I have a script that works fine on my test server (using IIS6). The
I have a script that successfully encrypts a credit card. I need it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.