Sponsored Content
Top Forums UNIX for Dummies Questions & Answers passing grepped date var into code Post 302709113 by bdby on Tuesday 2nd of October 2012 05:14:53 PM
Old 10-02-2012
passing grepped date var into code

Hi Everyone,
I am a bit new at learning this bash syntax. I have a problem at work that needs to be addressed. We find we are spending quite a bit of time killing old processes created by oracle replication that have been restarted later in the week. Because the replication takes time to complete (in days) I want to kill those processes that are 4 days and older.

I found this bit of code that will work just fine but I can’t seem to hook it up to my ps statement and I am at a loss as how to do this.
NOTE: the grep word “hald” is actually one of the linux processes. Something to work with.
There are two variables the first one (print $2) is the pid and the second is the date ($9). The numbers 2 and 9 are the columns numbered left to right. That output looks like this:
Code:
[root@localhost script]# ps aux | grep -i "hald" | awk '{print $2 "   " $9}'
1958   10:56
1959   10:56
1999   10:56
2004   10:56
4729   13:39

I am trying to pass in the date ($9) to the “todate” but am getting confused with the available formatting.
Once I get this to work returning the number of days then I can test it and issue the kill command for the associated pid ($2).

Running the code alone with the dates already there works fine.
The default date format is the following: Tue Oct 2 14:04:31 PDT 2012 . All I want is the day which is represented by date +%d and this returns 02. How can I represent $9 as date +%d ??
Any reference book recommended for reading would be appreciated.
Code:
#!/bin/sh
ps aux | grep -i "hald" | awk '{print $2 " " $9}'  
|
fromdate=01.04.2010
todate=24.05.2010
from=`echo $fromdate | awk  -F\. '{print $3$2$1}'`
to=`echo $todate | awk  -F\. '{print $3$2$1}'`
START_DATE=`date --date=$from +"%s"`
END_DATE=`date --date=$to +"%s"`
DAYS=$((($END_DATE - $START_DATE) / 86400 ))
echo $DAYS

Moderator's Comments:
Mod Comment Please use code tags, avoid odd font settings thanks.

Last edited by jim mcnamara; 10-02-2012 at 06:21 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Date as parameter with sh script

Hi I need to pass a date argument with my shell script. Can any one send me code for the same. eg For 15th Aug 2006. Iwant to pass parameter like this ./startenv.sh 08/15/2006. Everyday date argument must change. Will the below code work ? ./startenv.sh date '+%m/%d/%y' THanks... (1 Reply)
Discussion started by: pankajkrmishra
1 Replies

2. Shell Programming and Scripting

passing date parameter in cronjob

Hi All, I have a crontab entry for every 5 min running. 5 * * * * /tmp/scripts/ksh/CsVues_H.sh `date +%Y%m%d_%H` >> /tmp/scripts/ksh/Cronresult.log and I am passing the date parameter. ( `date +%Y%m%d_%H` ) But the parameter values which i am expecting inside the script is... (6 Replies)
Discussion started by: scorpio
6 Replies

3. Shell Programming and Scripting

Unix Script to email grepped results

I am following the tread number 81556 to grep a file and send the results in email. #/bin/bash /bin/grep -i 'invite sip' /var/log/asterisk/full if echo "found" | mail -s 'invite sip' mail@gmail.com When I just build it to grep and mail, it works fine. However, the if statement causes... (4 Replies)
Discussion started by: klysdale
4 Replies

4. Solaris

SFTP passing variables date

Hi, Anyone can help me on how to solve my problem not getting the actual $DATE saying . Here my scripts; #!/bin/sh DATE='20110331' sftp -oUserKnownHostsFile=/.ssh/known_hosts -oIdentityFile=/.ssh/id_rsa -b /source/transfer.sh server1@sftp.com <<EOF #tranfer.sh put... (3 Replies)
Discussion started by: fspalero
3 Replies

5. Shell Programming and Scripting

Passing CURL a date argument (formatting)

Im trying to pass curl a list of arguments... one of which is a date... When I run from the command line it works fine but when i try to run the same from a script passing variables it reformats the date for some reason and doesn't work. Example: curlstring=xxxxxxxxxxx.xxx.xxx:8090/csv/... (1 Reply)
Discussion started by: atelford
1 Replies

6. Shell Programming and Scripting

Replacing grepped text.

Dear Friends, I need your help once again. I have a flat file from which I have grepped something and kept in a separate file for some reason. I want to replace these grepped srtings with the strings in another file. E.g. Actual_file.txt COLOR: 33179 Lakme SEPT2011 Lot No: BR25324 ... (5 Replies)
Discussion started by: anushree.a
5 Replies

7. Shell Programming and Scripting

Passing date into awk...

Hi All.... I need to pass date into awk and parse logfile based on that.... i used both awk and /usr/xpg4/bin/awk... both are throwing up error..... So here is the stuff... when i use /usr/xpg4/bin/awk : DATE=`date '+%Y %b %d'` START=00 END=23 /usr/xpg4/bin/awk -v DATE={"$DATE"} -v... (3 Replies)
Discussion started by: Nithz
3 Replies

8. Shell Programming and Scripting

How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number. For Example: #!/bin/ksh echo "Enter RRS ID: " read rrs echo "Enter error number:" read err scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/... (7 Replies)
Discussion started by: JayDoshi
7 Replies

9. Shell Programming and Scripting

Passing PERL var values to SH Shell Script

Greetings all, If I have a SH script that calls a PERL script in the following way: perl $HOME/scripts/config.properties And in the config.properties PERL file, this Perl script only sets a number of environmental parameters in the following way: #!/usr/bin/perl $VAR1 = ( ... (3 Replies)
Discussion started by: gikyo12
3 Replies

10. UNIX for Dummies Questions & Answers

Print the grepped string alongwith the filename

We have C shell and we are executing the below script: #!/bin/csh -f if ($#argv != 2) then echo "Usage $0 DirecotryPath Inputfilename" exit 1 endif set dir=$1 set fname=$2 echo $dir foreach line ( `cat $fname` ) echo \ ======================================== >>... (2 Replies)
Discussion started by: donisback
2 Replies
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 02:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy