Visit Our UNIX and Linux User Community


Why stderr file descriptor redirection makes ksh's "select" construct hang.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why stderr file descriptor redirection makes ksh's "select" construct hang.
# 1  
Old 05-06-2010
Question Why stderr file descriptor redirection makes ksh's "select" construct hang.

I am trying to use one global declaration --> "exec 2>$ERR" to capture all stderr outputs that may occur anywhere in my script.
Then close it at the end of the script using --> "exec 2<&-"

I am using KSH on Solaris 8.
KSH Version M-11/16/88i

If I comment two "exec .." statements in the script,, script[select] works fine.. but if I use --> "exec 2>$ERR" script[select] is hanging.

Quote:
test.sh
Code:
#!/usr/bin/ksh

ERR=all_script_errors.log

# 1. My intent here is to use one global declaration to capture stderr outputs that may occur anywhere in my script.
# 2. For the scope of this script(PID), stderr will be routed to ERR file
exec 2>$ERR

PS3='Select an option and press Enter: '
select i in Date Host Users Quit
do
   case $i in
      Date)  date;;
      Host)  hostname;;
      Users)  who;;
      Quit)  break;;
   esac
done

# close/Reset stderr global error redirection.
exec 2<&-

## EOF

Please try the script on your end..
# 2  
Old 05-06-2010
try running the script with sh -x scriptname you will get an idea :-)
# 3  
Old 05-06-2010
Extract from "man ksh".

Code:
    select identifier [in word...] do list done
                     A select command prints on standard error (file
                     descriptor 2),


You are intercepting standard error.
Just for interest if you type "4" while it is apparently hung, the script exits tidily and the menu text appears in the log file.
# 4  
Old 05-06-2010
Hi Vidyadhar here is the output..

Code:
$> sh -x test.sh
ERR=errors.log
+ exec

I am asking experts here to try the code because its tricky..
Here is the same code without "select" construct..

Quote:
test1.sh
Code:
#!/usr/bin/ksh
set -vx

ERR=all_script_errors.log

# For the scope of this script(PID), stderr will be routed to ERR file 
exec 2>$ERR

blabla

# Reset error redirection. Since select construct's output is going to STD_ERROR.
exec 2<&-

## EOF


Quote:
This output clearly shows there is nothing wrong with stderr redirection part of the logic..,

Code:
$>more all_script_errors.log
test2.sh[9]: blabla:  not found

Please try the code on your end..

---------- Post updated at 11:42 AM ---------- Previous update was at 11:27 AM ----------

Methyl you are right,, Select statement is printing menu choices to stderr.

Quote:
After I closed stderr redirection,, just before select construct gets executed, now select menu is neither printing output to console nor to $ERR ..!!
Does that mean my stderr descriptor closing(exec 2<&-) is wrong?

Code:
#!/usr/bin/ksh

ERR=all_script_errors.log

# For the scope of this script(PID), stderr will be routed to ERR file 
exec 2>$ERR


# Reset error redirection. Since select construct's output is going to STD_ERROR.
exec 2<&-

PS3='Select an option and press Enter: '
select i in Date Host Users Quit
do
   case $i in
      Date)  date;;
      Host)  hostname;;
      Users)  who;;
      Quit)  break;;
   esac
done

## EOF

<QUOTE>
$>more all_script_errors.log
<None>
With --> "exec 2<&-" statement at the end of the script,, I get following output confirming your observation..

Quote:
$> more all_script_errors.log
1) Date
2) Host
3) Users
4) Quit
Select an option and press Enter: Select an option and press Enter: Select an option and press Enter: Select an option and press Ent
er:
[CODE]

Last edited by kchinnam; 05-06-2010 at 01:47 PM..
# 5  
Old 05-07-2010
Please could someone check -->
why even after closing stderr File Descriptor using (exec 2<&-), select construct could not print to console ?
# 6  
Old 05-07-2010
Quote:
Originally Posted by kchinnam
Code:
# Reset error redirection. Since select construct's output is going to STD_ERROR.
exec 2<&-

That does not reset stderr to its original location. That closes the file descriptor and it can no longer be used.

The most correct approach is to "save" the original destination of stderr in case you need it later. For example:
Code:
exec 3>&2                  # File descriptor 3 now points to the original destination of stderr
exec 2 > someotherplace    # Standard error is now redirected for all commands that follow
select ...
do
...
done 2>&3    # Select's stderr is redirected to the original stderr destination, while
             # all other commands continue to redirect stderr to "someotherplace".

Regards,
Alister

Last edited by alister; 05-07-2010 at 04:06 PM..
# 7  
Old 05-07-2010
Alister, your suggestion is working..

How can I reset only this--> exec 2>$ERR, and not worry about breaking script that follows by closing stderr --> exec 2<&- ?

Is there a command that I can use to check if stderr is closed ?

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Solaris

"Estream construct failed" Error on Solaris i86pc

Hi Guys, From past some days, I am getting an error in /var/adm/messages which is as shown below. XXXXX02:/# cat /var/adm/messages |tail Sep 16 15:28:14 XXXX02 EV_AGENT: Agent Main --Estream construct failed. Err: EMULSocket::recv() Sep 16 15:31:49 XXXX02 EV_AGENT: Agent main --... (2 Replies)
Discussion started by: vivek.goel.piet
2 Replies

4. UNIX for Dummies Questions & Answers

File name select to "clipboard"

I want to be able to take a file name and then use sedto paste that text into the file. I believe I know how to do the latter portion of pasting into the file at the location I want, but I do not know how to "copy" the file name to the "clipboard" for pasting. Ideally I want to be able to... (1 Reply)
Discussion started by: TitanTlaloc
1 Replies

5. Shell Programming and Scripting

(CSH basic construct) "@" query

While going through a script, i came across few syntax which I am not aware of what they exactly means. @ cnt = 1 @ num_all = `echo $var` What is the significance of "@" here. What is it called? (3 Replies)
Discussion started by: animesharma
3 Replies

6. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Compare file names and select correct elements to include in "for each loop"

Hi everyone, I`ll try to be most clear I can explaining my help request. I have 2 folders Folder A-->This folder receives files through FTP constantly Folder B-->The files from Folder A are unzipped and then processed in Folder B Sometimes Folder A doesn`t contain all... (2 Replies)
Discussion started by: cgkmal
2 Replies

9. Shell Programming and Scripting

Question on using "[[ ]]" in lieu of IF Construct

Unix gurus, I have a piece of code as below. ] && INST="${ORACLE_SID}" || INST="${TWO_TASK}" I know that the above code can be used in lieu of an IF construct. I also know that the above code can be extended for the "true" condition to include more than one command (as below): ... (8 Replies)
Discussion started by: sunpraveen
8 Replies

10. HP-UX

script running with "ksh" dumping core but not with "sh"

Hi, I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone. And its not dumping core if we run the script using "/bin/sh" instead of "ksh" Can some body please help me how to resolve this issue. ... (9 Replies)
Discussion started by: simhe02
9 Replies

Featured Tech Videos