【问题标题】:What changes the result of the 'tput lines' command?什么会改变“tput lines”命令的结果?
【发布时间】:2021-09-30 20:23:56
【问题描述】:

我在 LINES=50 的终端上的 bash 上执行了以下代码。 'tput lines' 命令的结果各不相同。我不知道解释这个结果。为什么 'tput lines' 命令可能返回 '24' ?

代码:

#!/bin/sh
echo $LINES
tput lines
tput lines 2>/dev/null
echo $(tput lines)
echo $(tput lines 2>/dev/null)
true && tput lines
true && tput lines 2>/dev/null
true && echo $(tput lines)
true && echo $(tput lines 2>/dev/null)
echo $TERM
echo $TERM 2>/dev/null
echo $(echo $TERM)
echo $(echo $TERM 2>/dev/null)
true && echo $TERM
true && echo $TERM 2>/dev/null
true && echo $(echo $TERM)
true && echo $(echo $TERM 2>/dev/null)

结果1:

$ ./testtput

50
50
50
24
50
50
50
24
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color

结果2:

$ ./testtput | pbcopy

50
24
50
24
50
24
50
24
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color
xterm-256color

我的环境:

  • Mac mini 2018 年末
  • MacOS 11.4
  • “uname -a”的结果是 Darwin hogehoge.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64
  • $BASH_VERSION 是 3.2.57(1)-release

【问题讨论】:

    标签: bash macos macos-big-sur tput


    【解决方案1】:

    当标准输出连接到终端时,tput 将查询终端设备以获取窗口尺寸。

    当您将tput 放入命令替换$(tput lines) 时,它的标准输出连接到管道,而不是终端。在这种情况下,它将使用LINES 环境变量,或者终端类型的terminfo 条目中的默认大小。

    由于您有 LINES=50 但在使用管道时没有得到它,我怀疑您忘记导出变量。使用

    export LINES=50
    

    然后再试一次。

    更新:

    我误读了这个例子(很难说出每个输出都有哪些命令)。现在我认为当标准输出不是终端时,它会尝试标准错误。只有当它们都不是终端时,它才会回退到默认值。所以当标准输出是命令替换的管道时你会得到24,并且标准错误被重定向到/dev/null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多