Passing a file descriptor


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a file descriptor
# 1  
Old 06-04-2008
Question Passing a file descriptor

I am trying to right a function which uses a file descriptor to write to a log file. The problem is that the on the print statement the file descriptor is called bad. Now when I first open the file and print to it in the f_open function by passing the descriptor to f_print_log all works well, however when I returned the file descriptor to logtest.sh and then try to pass it to f_print_log I get bad file descriptor. Any help would be appreciated. Here are my functions and calling script:

********* THESE FUNCTIONS ARE IN f_log.sh
function f_open_log
{
next_fh=$LOG_FH_COUNTER
eval "exec $next_fh>$1"

f_print_log $next_fh "Log file opened"
(( LOG_FH_COUNTER=LOG_FH_COUNTER + 1 ))

echo ${next_fh}
return 0
}

function f_print_log
{
print -u$1 $2
return 0
}
***************************************************

logtest.sh -->

#!/bin/ksh

. f_log.sh

typeset -i LOG_FH_COUNTER=3

LOG=$(f_open_log bigfile)
f_print_log $LOG "This is a test"

exit
# 2  
Old 06-04-2008
Which particular version of ksh are you using? ksh88, phksh or ksh93?
# 3  
Old 06-05-2008
Thanks for responding. I am running ksh88.
# 4  
Old 06-05-2008
I found what is causing the bad file descriptor, however I do not know how to get the funcitonallity I want to work. I left a line out out of the f_print_log function as I was trying not to make my post to long. The function should be

function f_print_log
{
[[ $VERBOSE == 0 ]] && print $2
print -u$1 $2
return 0
}

So if I set the VERBOSE option in logtest.sh, output would be written to the screen and the file, however when f_open_log is called and it writes the "Log file open" using the f_print_log subroutine both the file descriptor and that line are sent back to logtest.sh which generates the bad descriptor.

Any suggestions. I would like to keep the Log file opened line if I can. Thanks.
# 5  
Old 06-05-2008
I don't think the descriptor will persist in the parent shell when you run it from inside backticks. At least that's the case here, with Bash.
# 6  
Old 06-06-2008
Found a solution. I added an additional argument to the f_print_log routine to turn verbose off. I use this in the f_open_log routine and everything works fine.
# 7  
Old 06-08-2008
The problem is with the following line of code
Code:
LOG=$(f_open_log bigfile)

This causes the file descriptor to be closed. One workaround is to use a global variable as in the following example
Code:
#!/bin/ksh88

typeset -i fh

function open_log
{
    next=${LOG_FH_COUNTER}
    eval "exec ${next}>$1"
    fh=${next}

    print_log ${next} "Log file opened"
    ((LOG_FH_COUNTER=LOG_FH_COUNTER+1))

    echo ${next}
}

function print_log
{
    print -u$1 $2
}

typeset -i LOG_FH_COUNTER=3
open_log bigfile
print_log ${fh} "This is a test"

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable File Descriptor

Greetings. I am happily using constructs like the following; I have set -x to show fine distinctions. $ exec 4> afile + exec + 4> afile $ print -u4 This is the first line in afileAs you can see from the -x expansion, the shell performed the exec command and redirected file descriptor to... (4 Replies)
Discussion started by: rpaskudniak
4 Replies

2. Shell Programming and Scripting

file descriptor count

I am trying to write a script which will only show me the file descriptor count for a process/pid. My script will return me the count only not the whole output. For example, I would like my script to return the output 23 this case, not the whole output. Can anybody please help me how do I get... (11 Replies)
Discussion started by: mohullah
11 Replies

3. UNIX for Dummies Questions & Answers

File Descriptor

Hi What the below path contains? /proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

4. Shell Programming and Scripting

File Descriptor

Hello All, Im opening a file desciptor in perl and sending data using print CMD "$xyz". is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies

5. UNIX for Advanced & Expert Users

File Descriptor Table

Im working on writing a small operating system. I am currently working on implementing dup, dup2, pipe, and close and I need to implement some type of file descriptor table in my PCB. I was wondering if there is anyone who is familiar with linux/unix implementation of these tables who could... (6 Replies)
Discussion started by: Ashaman0
6 Replies

6. Programming

File descriptor constant

I have a requirement to close all the file descriptors from 3 to 1024 for a particular application. Right now, this is how I do it .. for ( int i = 3 ; i <= 1024; ++i ) close(i); The change I am looking at is, I want to do away with the number 1024 and replace it with a constant which... (4 Replies)
Discussion started by: vino
4 Replies

7. Programming

Problems with file descriptor

Hi, look at the following code: The client after estabilishing a connection with the server does the following: if ((peter = fopen(argv, "r")) == NULL){ printf("errore\n"); exit(0); } ... (11 Replies)
Discussion started by: teo
11 Replies

8. UNIX for Dummies Questions & Answers

File Descriptor Help

What is a file descriptor in Unix?? How to find a file descriptor of a file in Unix?? Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies

9. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies

10. UNIX for Dummies Questions & Answers

bad file descriptor?

Ok, I'm sure this is a total newbie question, but I think I'm in the right place, no? I'm trying to call a perl module from a cgi script - Mail::Sendmail - and my web host installed the module in a directory that doesn't seem to be accessible, at least not the way I'm trying. But I thought you... (1 Reply)
Discussion started by: ftb
1 Replies
Login or Register to Ask a Question