Register
It is currently Thu Jul 31, 2014 11:36 am

redirect output of "select" to stdout instead of stderr?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Jun 09, 2011 8:21 am   

Joined: Thu Jun 09, 2011 7:59 am
Posts: 2
To avoid having to constantly redirect stderr, I usually exec it to a log file for the entire script at the start of the script. But, unintuitively, the "select" command writes its output to stderr instead of stdout, which prevents me from using that method.

Does anyone know why "select" writes to stderr instead of stdout?

Is there a way to have it use stdout instead?

To illustrate the problem, here's a small script. Uncomment the "exec" lines to see the behaviour.
---- begin test script
#!/bin/bash

#exec 2> x.log
date
lshlsfh

PS3="Option: "
OPT_L="\"option 1\" \
\"quit\""

eval set $OPT_L
select OPT in "$@"; do
case $OPT in
"option 1")
echo "you selected option 1"
break
;;
quit)
echo "you selected quit"
break
;;
esac
done

#exec 2>&-
exit
---- end test script

Kind regards,

Herta


Top
 Profile  
 PostPosted: Fri Jun 10, 2011 8:12 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Nope, I don't know why select(0) redirects to STDERR (as « python --version »). Though, you can avoid this by redirecting select(0)'s file descriptor 2 to file descriptor 1.

Also, you should never use eval(0), and set(0) in this case is useless : you're not writing for POSIX sh, so use arrays ! They're your friends.

Here is how I would write your code :

Code:
#!/bin/bash
exec 2>"$0".errlog

date
lshlsfh

PS3='Option: '
opts=('option 1' quit)

select opt in "${opts[@]}"; do
    case "$opt" in
        "${opts[0]}") printf 'you selected option 1\n' ;;
        "${opts[1]}") printf 'you selected quit\n'
    esac

    break
done 2>&1

exec 2>&-


Last edited by jsz on Sun Jun 19, 2011 9:50 am, edited 2 times in total.

Top
 Profile  
 PostPosted: Sat Jun 11, 2011 3:26 pm   

Joined: Thu Jun 09, 2011 7:59 am
Posts: 2
Nice!

Thanks, jsz.


Top
 Profile  
 PostPosted: Sun Jun 19, 2011 7:29 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
herta wrote:
Does anyone know why "select" writes to stderr instead of stdout?

Because, usually, the stdout of a script is what one is interested in :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP