#!/bin/csh -f # acclog_filter_grep # ----------------------------------------------------------------------------- # C shell script browse the acclog, filtering out my IP address, and # translating some known IP addresses to meaningful names. Useful as: # ssh remote_host acclog_filter_grep # ----------------------------------------------------------------------------- # Usage: Run with -h or --help option to see usage. # Assumptions: # Effects: # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- # Collect command line options set option_quick_string = "" set option_all_string = "" set option_noless = "false" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t [options]" echo "Options:" echo " -h = Show this help text" echo " --help = Show this help text" echo " -q = Quick. Most recent log only" echo " --quick = Quick. Most recent log only" echo " -a = All logs" echo " --all = All logs" echo " -n = Not piped through less" echo " --noless = Not piped through less" exit 1 else if ("$1" == "-q" || "$1" == "--quick") then set option_quick_string = "$1" shift else if ("$1" == "-a" || "$1" == "--all") then set option_all_string = "$1" shift else if ("$1" == "-n" || "$1" == "--noless") then shift set option_noless = "true" else if ("-" == "`echo $1:q | cut -c 1`") then echo "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first argument break endif end # Note: Have to repeat the full command in both if and else branches. # Can't find a way to store the search option for less in a # variable with value "" when --noless and "+/$*" otherwise. # Always end up with either: # - $* not expanded, so it searches for a dollar sign and a star. # or: # - The quotes stripped off so when multiple args are specified # by the caller, the first is searched for and the others are # erroneously treated as filename ags to less. And the same # when a single quoted multi-word arg is passed by the caller. # I tried various combinations of delimiters, including single # quotes, double quotes, parentheses, with and without backslashes. # Never found a way. # In similar scripts acclog and acclog_filterm there is no # searching being done, so I can get away with defining $less_cmd # as "less" or "cat" and just piping through $less_cmd. Can't do # that here because I want to pass the same search string to less # as I passed to grep. Otherwise, less strips off the color # highlighting of search matches and doesn't add its own highlighting. # The best alternative would be if there were an option for less # that told it to not strip off grep's color highlighting. Could # then hardcode that option as part of $less_cmd and avoid this # whole if statement. Doesn't seem to be such an option though. # With the current solution, both of the following commands # % acclog_filter_grep a b c # % acclog_filter_grep "a b c" # correctly search for the string "a b c". # Note: Use "$*", not $*:q, or multiple args and multi-word args don't work. # If they're not all in one string, words beyond the first are # treated as filename args to grep, not the string to be searched. if ($option_noless == "true") then acclog_filter $option_quick_string $option_all_string --noless \ | grep -i --color "$*" else acclog_filter $option_quick_string $option_all_string --noless \ | grep -i --color "$*" | less "+/$*" endif