Results 1 to 4 of 4
So the goal of this script is to figure out when a script has started or ended by comparing two intermediate files
The first scan of the ps will always ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-23-2013 #1Just Joined!
- Join Date
- Jan 2013
- Posts
- 9
Help with awk
So the goal of this script is to figure out when a script has started or ended by comparing two intermediate files
The first scan of the ps will always be marked with as started so I used the following code to accomplish that:
The next part is tricky I need to compare the two scans and if it started then it prints out : Started: user pid commandCode:echo "DATE: $CURRENT" > spy.txt ps axo user,pid,comm >> spy.txt echo " " echo " " > current.txt for array in ${array[at symbol]} #still cannot print at symbols in posts do grep "$array" spy.txt >> current.txt done head -1 spy.txt >> out.txt for array in ${array[at symbol]} do grep "$array" current.txt | sed "s/^/Started:/">> out.txt done
If it ended it should print out : Ended: user pid command
If it did nothing then all it should do is print the date of the scan and a blank line:
Date
I thought I could use the following code to show the differences :
But all that does is print that it both started or endedCode:awk 'NR==FNR{a[$1]++;next;}!($0 in a)' last.txt current.txt|sed "s/^/Ended:/" >> out.txt awk 'NR==FNR{a[$1]++;next;}!($0 in a)' current.txt last.txt|sed "s/^/started:/" >> out.txt
Is there a better way to do this?
Is there some loop or if logic I could use to implement this section?
- 02-24-2013 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,680
Hi,
Quite apart from your awk question, what exactly are you trying to accomplish? All your output redirections make me queasy.
Also, unless you are not showing some of your code, that @array is not defined yet, so neither of those loops would run anyway (as shown).Last edited by atreyu; 02-24-2013 at 02:00 AM. Reason: typo
- 02-24-2013 #3Just Joined!
- Join Date
- Jan 2013
- Posts
- 9
Well the code is quite unsettling as I am quite a novice, but the goal is to run the ps command and search the scans for comnmand line strings. Then each time the loop runs the last scan and the first scan are compared and the results are output in a file. The output has to be formatted in a way that if there is a difference between the scans the output has to say
started user, pid, comm
ended user, pid, comm
And if there is no difference the output is just the date of the scan
I can include my entire code, but it is not pretty and will probably make you more queasy
you run the command as such:Code:#!/bin/bash trap "rm psmonitor.txt; echo Interupt encountered, exiting; exit 1" SIGHUP SIGINT SIGTERM #trap to catch interupts , deletes output and prompts user with exit condition CURRENT=$(date) usage(){ echo "usage $0 -t[tseconds] -n[count]" } #this function will print out the correct usage when the shell is run incorrectly COUNT=5 TIME=1 echo " " > out.txt until [[ $1 == -* ]] do array+=( "$1" ) shift done while getopts :t:n: option #getopts to pass arguments to the script do case "${option}" in t) TIME="$OPTARG";; n) COUNT="$OPTARG";; \?) case "$OPTARG" in t) echo " missing time parameter." ;; n) echo " missing count parameter." ;; \?) echo "Error: unknown option -$option";; #catch all error message esac usage #print usage to screen exit 0 ;; esac done echo "DATE: $CURRENT" > spy.txt ps axo user,pid,comm >> spy.txt echo " " echo " " > current.txt for array in ${array[at symbol]} do grep "$array" spy.txt >> current.txt done head -1 spy.txt >> out.txt for array in ${array[@]} do grep "$array" current.txt | sed "s/^/Started:/">> out.txt done let 'COUNT=COUNT-1' mv current.txt last.txt loop=1 while [ $COUNT -gt 0 ]; do echo "DATE: $CURRENT"> spy.txt ps axo user,pid,comm >> spy.txt #run command ps -e to list all process and append it to thelog file echo " " echo " " > current.txt for array in ${array[at symbol]} do grep "$array" spy.txt >> current.txt done head -1 spy.txt >> out.txt awk 'NR==FNR{a[$1]++;next;}!($0 in a)' last.txt current.txt|sed "s/^/Ended:/" >> out.txt awk 'NR==FNR{a[$1]++;next;}!($0 in a)' current.txt last.txt|sed "s/^/started:/" >> out.txt mv current.txt last.txt let 'COUNT=COUNT-1' #count down mechanism for loop sleep $TIME #argument to wait so many seconds between iterationso fo main loop done
./spy [list of parameters] -t<seconds> -n <count>
The list of parameters is handled by sending the arguments into an array
Scripting is not my strong suit as you can tell
- 02-25-2013 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,680
What is meant by "command line strings"? I guess this is what is in @array? Can you show an example of what that might be?
What is the point of this? Can you show an example of what these first and last scans might look like?Then each time the loop runs the last scan and the first scan are compared and the results are output in a file.
I still don't understand what you are after, to be honest. I know you said you want to know if a script started or ended, but is there more to it than that?


Reply With Quote

