Read loop from two files using descriptors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read loop from two files using descriptors
# 1  
Old 06-30-2014
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:
Code:
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;
Code:
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.

Code:
#!/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

Moderator's Comments:
Mod Comment CODE tags are for sample input and output as well as for code segments.

Last edited by Don Cragun; 06-30-2014 at 03:54 PM.. Reason: Add CODE tags.
# 2  
Old 06-30-2014
Your while loop reads one line from each file and thus works on these pairs only. To have it compare/check the entire grplist file against every single job id, you need two nested loops, reading through the entire grplist file for every line in atdinfile. Or, you can use grep -f grpfile atdinfile.
# 3  
Old 06-30-2014
Thanks for your reply.

I don't think the grep -f option will work in this case because I'm grepping on stdout from the at command and not to inside of a file during the loop.

Can you give me an example of how two nested loops might look like given this scenario?
# 4  
Old 06-30-2014
You can grep -fgrplist on stdin from a pipe.

Nested loops look like e.g.
Code:
while read atd
   do while read grp
         do  your_commands
         done < GRPLIST
   done < ATDINFILE

The outer loop reads the jobs one by one, and the inner does the comparisons for the entire grp file for every single job.
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 06-30-2014
Thank you. I will give this a try.
# 6  
Old 07-01-2014
Quote:
Originally Posted by RudiC
You can grep -fgrplist on stdin from a pipe.

Nested loops look like e.g.
Code:
while read atd
   do while read grp
         do  your_commands
         done < GRPLIST
   done < ATDINFILE

The outer loop reads the jobs one by one, and the inner does the comparisons for the entire grp file for every single job.


Thanks again as this snippet got me on the right track..


Here's what I ended up using.

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Read .xlsx and text files in a loop using openpyxl

I have a list of .xlsx files (names 1.xlsx, 2.xlsx etc) in a directory, on which I need to insert data from its corresponding text file (named 1.txt, 2.txt etc) on the second worksheet named 'Filtered' and save it. The code I am trying is #!/usr/bin/python import os from... (8 Replies)
Discussion started by: nans
8 Replies

2. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

3. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

4. Shell Programming and Scripting

Read and edit multiple files using a while loop

Hi all, I would like to simply read a file which lists a number of pathnames and files, then search and replace key strings using a few vi commands: :1,$s/search_str/replace_str/g<return> but I am not sure how to automate the <return> of these vis commands when I am putting this in a... (8 Replies)
Discussion started by: cyberfrog
8 Replies

5. Shell Programming and Scripting

until loop and read

Hi guys what I wanna do is to create a script where can I input several times a file - assume with read function that will be inserted into a temp. file for further processing. When I press q I want that the loop will stop and continue in my script I typed this but the options q is not working... (6 Replies)
Discussion started by: kl1ngac1k
6 Replies

6. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies

7. UNIX for Dummies Questions & Answers

how to access files via their descriptors?

hello, I'm new here, and new to Linux. I was wondering how to get hold of a certain file's descriptor, and how to use that number so that I access the contents of the file. please give me a full example, that is (1) how to determine the descriptor and (2) what magic symbols to use so that... (4 Replies)
Discussion started by: ialoq
4 Replies

8. UNIX for Dummies Questions & Answers

Write/read to file descriptors

Is it possible to write to file descriptor 0 and read from 1 or 2? How could this be implemented? (3 Replies)
Discussion started by: machshev
3 Replies

9. UNIX for Dummies Questions & Answers

how to list the files using File Descriptors

hello, I have written a script named listall.sh with the following codes init. #!/bin/bash PATH="/proj/cmon/$1" echo $PATH if ; then echo "Usage: $0 ***" exit 1 else ls -l $PATH/*.sc fi Here there are 3 subdirectories (namely - src, data and jobs)under /proj/cmon, so... (2 Replies)
Discussion started by: shyjuezy
2 Replies

10. Shell Programming and Scripting

help with a 'while read' loop to change the names of files

Hi, I am new to scripting, so any help on this would be much appreciated. I am trying to rename a bunch of files, taking the names sequentially from a list read in another file... # ls oldnames file_1 file_2 file_3 # cat names red yellow green I want the files to take on the... (6 Replies)
Discussion started by: starsky
6 Replies
Login or Register to Ask a Question