[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: rpm check script problem



On Monday, Oct 9th 2006 at 16:11 +0100, quoth Dan Track:

=>I've got a script that should check which rpms are installed on the
=>system. Part of the script involves using file descriptors. I'm having
=>trouble understanding these file descriptors and was hoping someone
=>can clarify my mistake. As a simple script I wrote the following:
=>
=>#!/bin/bash
=>$filename=$1
=>$filename1=$2
=>
=>exec 9<&0 < $filename			# (1)
=>read rpm
=>echo -e "file1: "$rpm "\c"
=>
=>exec 8<&0 < $filename1		# (2)
=>read rpm1
=>echo -e "file2: "$rpm1 "\c"
=>
=>read rpm2 <&9				# (3)
=>echo -e "file1: "$rpm2 "\c"
=>
=>Now my understanding is that the last read and echo statements should
=>print out what is stored in file descriptor "9" however the script
=>just hangs on the last read. Is there a reason for this? And how do I
=>fix it? I've searched and searched but can't find any reason why this
=>shouldn't work.
=>
=>Thanks
=>Dan

You didn't tell us what you're trying to accomplish.

(1) The exec command without a program to run is used to change 
redirection for the duration of the process. So what that does is to dup 
channel 9 to be a copy of channel 0. i.e., if you try to read from channel 
nine, you'll be reading just as if you were reading from stdin. The 
problem is that order is significant. *After* you did the dup you then 
said stdin should come from channel 0. If you want stdin to henceforth 
come from $filename, you should have said

exec < $filename 9<&0

The next read statment is reading from channel 0 so it reads the first 
line of $filename and puts it into the variable rpm.

(2) Again with the exec command. This time we dup channel 0 (which is 
already copied into 9) and set the copy into channel 8. The read gives me 
the first line of $filename1 and puts it into rpm1. But note that read 
ends up not using a duped copy of channel 8. See (1) above.

(3) Now we see the <&9 construct which is really shorthand for 0<&9 which 
dups 9 back onto 0 but only for the duration of the read.

Make sense?

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]