You are not logged in.

#1 2014-06-30 18:37:29

woodson2
Member
Registered: 2014-06-30
Posts: 5

Read loop from two files using descriptors

What I would like to do is read each line in the atdinfile:

A sample atdinfile would look like this:
651
652
653
654
655
656
657
658
659
660
661
664
665
666
667
668

I would like to grep through each atd job id looking for a match based on the input from another infile would is a listed of user groups:

A sample grplist file would look like this;
groupa
groupb
groupc
groupd
groupe

The code below will go through the list of atd jobs only once and try to match a corresponding line in the group file. This is not what I want. I want to grep for every group in each atd line before it moves onto the next atd job. Another problem is that the atd job list could be any number of lines whereas the grplist will tend to be static.

#!/bin/bash
set -x
ATDINFILE=/root/atdoutter.txt
DELTIMER=/root/delist.txt
USERNAME=ttimer
/usr/bin/atq | awk '{print $1}'|sort > $ATDINFILE
GRPLIST=/root/grouplist.txt
exec 3<$ATDINFILE
exec 4<$GRPLIST


while read atd <&3 && read grp <&4
do

at -c $atd|grep -wq "tmp-lock-ou.ldif."$USERNAME"" && at -c $atd | grep -wq "$grp"
 if [ $? -eq 0 ] ; then
   echo -e "${bldgrn}A $grp lock timer has been found for $USERNAME ${txtrst}" && echo "timer found for $USERNAME"
    sleep 1.5
    echo " "
     echo $atd | tee -a $DELTIMER >/dev/null 2>&1
      sleep 2
 fi
done < $ATDINFILE

Offline

#2 2014-06-30 22:14:15

Saint0fCloud
Member
Registered: 2009-03-31
Posts: 137

Re: Read loop from two files using descriptors

Not entirely sure what you want to do, but...

Why don't you just use command substitution for the atq command rather than redirect to a file? Then you would just loop through the grplist.txt file for every line from the atd command and you wouldn't have to open any file descriptors (which weren't being used properly in the first place).

In pseudo code

while read cline; do
    while read fline; do
        <pattern check>
    done < $GRPLIST
done < <(atq | awk '{print $1}')

Also when you're appending the output of $atd to $DELTIMER rather than use `tee` just use redirection (double `>>` to append).

echo $atd >> $DELTIMER

Offline

#3 2014-07-01 02:27:58

woodson2
Member
Registered: 2014-06-30
Posts: 5

Re: Read loop from two files using descriptors

Saint0fCloud wrote:

Not entirely sure what you want to do, but...

Why don't you just use command substitution for the atq command rather than redirect to a file? Then you would just loop through the grplist.txt file for every line from the atd command and you wouldn't have to open any file descriptors (which weren't being used properly in the first place).

In pseudo code

while read cline; do
    while read fline; do
        <pattern check>
    done < $GRPLIST
done < <(atq | awk '{print $1}')

Also when you're appending the output of $atd to $DELTIMER rather than use `tee` just use redirection (double `>>` to append).

echo $atd >> $DELTIMER

Ultimately I want to be able to run the atd command to list each atd job and then grep for each group inside of that job and perform another action if the exit status is 0.

For example using the atd job 651.

at -c 651 |grep groupa
if  { $? -eq 0 ] ; then
do something
fi

at -c 651 |grep groupb
if  { $? -eq 0 ] ; then
do something
fi

and so on and so on until each job id has been searched for each group.


Input file A
651
652
653
654
655
656
657
658
659
660
661
664
665
666
667
668


Input file B
groupa
groupb
groupc
groupd
groupe
groupf
groupg
grouph
groupi
groupj
groupk
groupl
groupm
groupn
groupo
groupp
groupq
groupr
groups
groupt

Offline

#4 2014-07-01 14:31:49

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Read loop from two files using descriptors

You don't really need to read each file at the same time. You want to find matches between the output of 'at' and contents of file B (right??). So...

while read -r uid; do
  if at -c "$uid" | grep -qwFf file_b; then
    : do something            
  fi                          
done <file_a

Offline

#5 2014-07-02 01:58:28

woodson2
Member
Registered: 2014-06-30
Posts: 5

Re: Read loop from two files using descriptors

falconindy wrote:

You don't really need to read each file at the same time. You want to find matches between the output of 'at' and contents of file B (right??). So...

while read -r uid; do
  if at -c "$uid" | grep -qwFf file_b; then
    : do something            
  fi                          
done <file_a


Thank you for the suggestion...

Here's what I ended up doing.

while read atdout; do

     while read accessgrp; do
        at -c $atdout | grep -wq "tmp-lock-ou.ldif."$USERNAME"" && at -c $atdout | grep -wq "$accessgrp"
            if [ $? -eq 0 ] ; then
               echo -e "${bldgrn}A $accessgrp lock timer has been found for $USERNAME ${txtrst}" && echo "Timer found for $USERNAME" > $TIMEROUT
                sleep 1.5
                  echo " "
                    echo -e "${bldylw}Displaying the current timer... ${txtrst}" && atq | grep $atdout | sed  's/^....//' |sed  's/......$//' | tee $LOCKTIME
                      sleep 3
                       echo " "
                       echo -e "${bldylw}Would you like to set a new timer for $USERNAME (yes/no)? ${txtrst}"
                       read input </dev/tty
                       if [[ $input = "yes" || $input = "YES" || $input = "y" || $input = "Y" ]]; then
                        echo $atdout | tee -a $DELTIMER >/dev/null 2>&1
                         do=enchilada
                         removetimer
                       elif [[ $input = "no" || $input = "NO" || $input = "n" || $input = "N" ]]; then
                         echo -e "${bldylw}Keeping old timer for $USERNAME ${txtrst}"
                          do=appetizer
                       else echo -e "${bldred}You entered an invalid option!${txtrst}"
                           cleanup
                       fi
            fi
     done < $GRPLIST
done < $ATDINFILE

Offline

Board footer

Powered by FluxBB