The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File Descriptor Table Ashaman0 UNIX for Advanced & Expert Users 6 12-03-2007 02:54 PM
File descriptor constant vino High Level Programming 4 05-18-2005 01:18 AM
File Descriptor Help rahulrathod UNIX for Dummies Questions & Answers 3 10-14-2004 05:08 AM
file activity (open/closed) file descriptor info using KORN shell scripting Gary Dunn UNIX for Dummies Questions & Answers 3 06-07-2004 01:54 PM
bad file descriptor? ftb UNIX for Dummies Questions & Answers 1 02-20-2002 07:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-04-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
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 (permalink)  
Old 06-04-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
Which particular version of ksh are you using? ksh88, phksh or ksh93?
  #3 (permalink)  
Old 06-05-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
Thanks for responding. I am running ksh88.
  #4 (permalink)  
Old 06-05-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
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 (permalink)  
Old 06-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 06-06-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
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 (permalink)  
Old 06-08-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0