LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2014, 01:20 PM   #1
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Rep: Reputation: 15
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.

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
 
Old 06-30-2014, 01:31 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Help us to help you. Provide two sample input files (10-15 lines will do). Construct a sample output file which corresponds to your sample inputs and post the samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin
 
Old 06-30-2014, 01:52 PM   #3
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by danielbmartin View Post
Help us to help you. Provide two sample input files (10-15 lines will do). Construct a sample output file which corresponds to your sample inputs and post the samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin
Daniel, thanks for your response. I'm looking to act on the each line in the files as I'm not really trying to construct a third output.

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
 
Old 06-30-2014, 02:47 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by woodson2 View Post
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.
Perhaps you want to use nested loops. The outer loop would run through every element in Input File A and the inner loop would run through every element in Input File B.

See (for an example) http://www.tldp.org/LDP/abs/html/nestedloops.html

Daniel B. Martin
 
Old 06-30-2014, 03:07 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
would grep -f help here ?
 
Old 06-30-2014, 07:37 PM   #6
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks for pointing me in the right direction. I'll look into both of these options.
 
Old 07-01-2014, 02:30 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
I "borrowed" one of the nested-loop examples from http://www.tldp.org/LDP/abs/html/nestedloops.html
to write this sample program.

With InFile1 ...
Code:
1
2
3
... and InFile2 ...
Code:
4
5
6
... this code ...
Code:
cat $InFile1 |        # Supply input from InFile1.
while read IF1value   # As long as there is another line to read ...
  do
    cat $InFile2 |        # Supply input from InFile2.
    while read IF2value   # Read one value at a time.
      do
        let "product=$IF1value*$IF2value"
        echo $IF1value "times" $IF2value "equals" $product
      done
  done >$OutFile
... produced this OutFile ...
Code:
1 times 4 equals 4
1 times 5 equals 5
1 times 6 equals 6
2 times 4 equals 8
2 times 5 equals 10
2 times 6 equals 12
3 times 4 equals 12
3 times 5 equals 15
3 times 6 equals 18
Daniel B. Martin
 
Old 07-01-2014, 04:55 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
First, the usual note of USE MORE QUOTES. Any unquoted expansion smells bad and can easily bite you down the road. One of the most important skills in bash is to have an instinctive wrongness reaction to unquoted expansions.

On the topic of the actual issue of nesting while-read loops, you can something like "5< file" to redirect the file into FD 5, and then use "read -u 5" to pull from that FD. This technique cuts out the Useless Use Of Cat.

Don't use ABS as a resource. It teaches bad style all over the place, and is only somewhat useful if you already know enough about bash to not really need it. http://mywiki.wooledge.org/BashGuide is far far better.

Last edited by tuxdev; 07-01-2014 at 04:57 PM.
 
Old 07-01-2014, 06:12 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by tuxdev View Post
First, the usual note of USE MORE QUOTES. ...
On the topic of the actual issue of nesting while-read loops ...
... ABS ... is only somewhat useful if you already know enough about bash ...
Thank you for the suggestions and pointers. I have little bash experience and that's the reason I had to "borrow" code from a web page prompted by a Google search.

In an earlier post I suggested nested do-loops, and the clumsy code in the follow-up post was an attempt to show the OP what nested do-loops look like.

It would be instructive if you reworked the code to show how it should be done. I hope you will do that.

Daniel B. Martin
 
Old 07-02-2014, 09:06 AM   #10
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks for all of the suggestions.

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
 
Old 07-02-2014, 09:38 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,836

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would rather collect and save the output of those at -c commands in a file and grep for the required info in it...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting nr of open file descriptors in processes without loop over fd-directory. herthh Linux - General 1 09-04-2011 12:55 PM
Want to read XP Event Viewer (& recover files if poss.) from reboot-loop harddrive GSGregg General 9 06-23-2011 08:50 AM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
how kernel resolves file descriptors when you do open() or read() alankar Linux - Kernel 2 09-04-2009 12:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration