How to compare previous and current item in for loop in bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare previous and current item in for loop in bash?
# 1  
Old 12-29-2016
Wrench How to compare previous and current item in for loop in bash?

Hey,
I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals.

At first I tried to store each log in and logo date in an array but it doesn't work. Can anyone help me?
# 2  
Old 12-29-2016
Give example data which you have tried to parse.
# 3  
Old 12-29-2016
Code:
loginn()
for logu in $(ls /tmp/$lognam/wtmp*)
do
logina=$(echo $(last -f $logu -F $name) | cut -c40-63)
    for loga in $logina
    do loginn[$i]=$(echo "$(date +"%s" -d"$loga" 2>/dev/null)")
    i=$(($i+1))
    done
done

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-29-2016 at 04:38 PM.. Reason: Added CODE tags.
# 4  
Old 12-29-2016
Here is example to calculate diff between login and session time ... easy to modify ..
using ksh93
Code:
#!/usr/bin/ksh
prev=""
name="someuser"

for logu in /tmp/$lognam/wtmp*
do
  last -f "$logu" -F "$name"
done  | while read line
do
 flds=($line)
        loginstart="${flds[4]} ${flds[5]} ${flds[6]} ${flds[7]}"
        loginend="${flds[10]} ${flds[11]} ${flds[12]} ${flds[13]}"
        [ "$loginstart" = "" ] && continue
        [ "$prev" = "" ] && prev="$loginstart" && continue
        epocnow=$(printf "%(%#)T" "$loginstart")
        epoclogout=$(printf "%(%#)T" "$loginend")
        epocprev=$(printf "%(%#)T" "$prev")
        ((logintime=epoclogout - epocnow))
        ((diff=epocprev-epocnow))
        ((h=diff/3600))
        ((s1=diff-3600*h))
        ((m=s1/60))
        ((s=s1-m*60 ))
        echo "$diff|$logintime|$prev|$loginstart|$h:$m:$s"
        prev="$loginstart"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash menu item counter

We have a simple menu with prompt of menu numbers to user. It is still under construction. Is there a way to "count" the menu choices so the prompt maximum count can be changed dynamically? See attached TODO note in code read_options(){ local choice # the... (7 Replies)
Discussion started by: annacreek
7 Replies

2. Shell Programming and Scripting

Unable to compare to a previous value of a variable in a while loop for a file

Hello All, I'm working on a script that will actually read a file consisting of data like the below:(ReportID,Sub_reportID,Sub_reportName) 1,1,ABC 1,2,DEF 1,3,GHI 2,1,JKL 2,2,MNO 3,1,PQR I want to read the Sub Report details for a Report_ID using while loop and write these values into... (6 Replies)
Discussion started by: venkat_reddy
6 Replies

3. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

4. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

5. Shell Programming and Scripting

Perl to send previous and current value

For example, I have a file called number.txt. x y 1 1 2 4 3 9 4 6 5 5 6 6 7 9 8 4 9 1 10 0 ... And I want to print out the value of x and y, if y%4==0 and the next value of y%4==0. Thus, the sample output is: 1 1 *because the previous x before 2 is 1 2 4 *because 4%4 == 0 7 9... (2 Replies)
Discussion started by: Tzeronone
2 Replies

6. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

7. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

8. Shell Programming and Scripting

AWK Compare previous value with current.

Hi, I have one small doubt how to go ahead and process the below requirement. File Content 1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: 1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 (3 Replies)
Discussion started by: dikesm
3 Replies

9. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

10. Shell Programming and Scripting

For loop - coping with an asterisk item

How do you get a for loop to cope with one of the items being an asterisk? for myResult in `echo "*"` do echo "$myResult" done The asterisk is returning a file listing in the PWD. The same result can be got from: for myResult in "*" do echo "$myResult" done (1 Reply)
Discussion started by: PaulUrwin
1 Replies
Login or Register to Ask a Question