修复了 tput:使用 SSH 运行远程 shell 脚本时没有 $TERM 值且未指定 -T

问题描述 投票:0回答:2

我在服务器上有一个nodetimecheck.sh 文件,其中有这样的命令

echo
tput setaf 2; echo -n " What is my node's local time: "; tput setaf 7; date

当我使用 SSH 登录到我的服务器并执行 ./nodetimecheck.sh 时,它会正确显示。

但是,如果我尝试像这样通过 ssh 从本地计算机执行命令

ssh -i ~/.ssh/privkey username@serverip ./nodetimecheck.sh

它确实显示时间,但有一条烦人的消息

tput: No value for $TERM and no -T specified

运行 Ubuntu 18.04 LTS 的本地计算机 GCP 上运行 Ubuntu 18.04 LTS 的远程服务器

ssh remote-access xterm tput
2个回答
0
投票

找到解决办法如下。提供 TERM=xterm 作为 ssh 命令的一部分

ssh -i ~/.ssh/privkey username@serverip TERM=xterm ./nodetimecheck.sh

0
投票

当没有可用的 TTY 时,您正在尝试执行

tput
命令。

真正的解决方案是检查命令是否在真实终端中执行:

#!/bin/sh
# a wrapper that checks whether TTY is available
t_put () {
  tty -s && tput "$@"
}
echo 
t_put setaf 2; echo -n " What is my node's local time: "; t_put setaf 7; date
© www.soinside.com 2019 - 2024. All rights reserved.