bash - repeat function with different arguments if one variable within the function is empty -
the function has check if target
empty, , if empty want repeated time c1time=$hours:$minutes:[0-9][0-9]
run it, , if still empty c1time=$hours:$c1minutes[0-9]:[0-9][0-9]
after should stop , echo info.
$1
standard logfile errors
$time
string time in pattern hh:mm:ss
sorry if confusing, i'm appreciate help
what function in detail:
first takes random time example 12:34:56
, reads "5" in c1seconds, c1time
12:34:5[0-9]
target tries find line string of c1time located in logfile , prints number of line.
c1date
changes 12:34:5[0-9]
in found time 12:34:56
example.
rest unimportant
like said above when target
empty has run again, how can check target while or after function executed?
function check() { c1seconds=`echo $time | awk '{print substr ($1,7,8)}' | head -1c` c1time=$hours:$minutes:$c1seconds[0-9] target=`cat -n $1 | grep "$c1time" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1` c1date=`cat $1 | grep "$c1time" | grep -eo "[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}" | head -1` exception=`tail -$[$window_lines-$target] $1 | grep -c exception` error=`tail -$[$window_lines-$target] $1 | grep -c error` semaphore=`tail -$[$window_lines-$target] $1| grep -c semaphore` echo $target return 0 }
well can use recursion. keep level=0
@ global level , call function again.
level=0; function check() {
now instead of
c1time=$hours:$minutes:$c1seconds[0-9]
use
if [ $level -eq 0 ]; c1time=$hours:$minutes:$c1seconds[0-9]; fi if [ $level -eq 1 ]; c1time=$hours:$minutes:[0-9][0-9]; fi if [ $level -eq 2 ]; c1time=$hours:$c1minutes[0-9]:[0-9][0-9]; fi if [ $level -ge 2 ]; echo "error"; exit; fi
now instead of
target=`cat -n $1 | grep "$c1time" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1`
use
target=`cat -n $1 | grep "$c1time" | awk '{printf "%s\n",$1}' | awk 'sub("$", "")'| head -1` if [ "x$target" = "x" ]; ((level++)); check $@; fi
i have not run typos may there, main idea number of recursion , checks @ max 3, use simple if check depth of recursion. can modify series of if
if else
. if grep
can open file, why cat file
, pipe it, , if awk
can search pattern in file, why cat file | grep pattern
before it?
Comments
Post a Comment