Execution strange when get lines of the console in script with command substitution and the rediretion of the stderr.
Configuration Information [Automatically generated, do not change]: Machine: i486 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i486' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i486-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../bash -I../bash/include -I../bash/lib -g -O2 -Wall uname output: Linux QGZ 3.0.0-1-686-pae #1 SMP Sat Aug 27 16:41:03 UTC 2011 i686 GNU/Linux Machine Type: i486-pc-linux-gnu Bash Version: 4.1 Patch Level: 5 Release Status: release Description: When I use "tput lines" to get the lines of console, it works, and also works in a script. And if execute the command in a script with the command substitution, it works well too. But if execute the script with the redirection "2>/dev/null", it won't work well. I'm not sure whether it is a bug or just my lack of some required knowledge. Please see one example below. Repeat-By: $ cat abc.sh #!/bin/bash tput lines echo $(tput lines) If execute the script with the content above, it is all right like : $ ./abc.sh 50 50 But if execute the script like this : $ ./abc.sh 2>/dev/null We will get : 50 24 I don't know why it is. What is amazing is the number 24. The number would never change no matter how I resize the console or other terminals. It also happens when a line of command in the script like "tput lines | cat". I haven't see the source code of BASH. I'm very sorry for having no good advice about this. And also sorry for my poor English. @_@
Re: Execution strange when get lines of the console in script with command substitution and the rediretion of the stderr.
On Fri, Nov 25, 2011 at 10:27:07PM -0500, Chet Ramey wrote: > >On 11/24/11 11:48 AM, QGZ wrote: > >> Bash Version: 4.1 >> Patch Level: 5 >> Release Status: release >> >> >> >> Description: >> When I use "tput lines" to get the lines of console, it works, and >> also works in a script. >> And if execute the command in a script with the command >> substitution, it works well too. >> But if execute the script with the redirection "2>/dev/null", it >> won't work well. >> I'm not sure whether it is a bug or just my lack of some required >> knowledge. >> Please see one example below. > >This doesn't have anything to do with bash. tput probably uses the >standard output as a handle to the terminal. When run in a command >substitution, stdout is a pipe. You might take a look at what tput >does using a system call tracer like truss or strace. > >Chet > Thanks for your suggestion! I have got what happened there with the help which is similar to yours from Jonathan Nieder : "Tput checks if the standard output (file descriptor 1) and standard error (file descriptor 2) streams are terminals and communicates with the terminal using whichever one is a tty." It's working now when executing in a command substitution in a script like this: echo $(tput lines 2>/dev/tty) Thank you! --QGZ