我正在为我的终端的PS1.
我正在使用设置颜色变量tput;例如,这里是紫色:
PURPLE=$(tput setaf 125)
Run Code Online (Sandbox Code Playgroud)
我如何找到125其他颜色的颜色代码(例如)?
某处是否有色表指南/备忘单?
我只是不确定什么125是......有什么方法可以将十六进制颜色转换为setaf可以使用的数字?
我遇到了一个问题,我试图通过使用脚本来获取终端的大小。通常我会tput cols在控制台内使用该命令,但是我希望能够通过严格使用脚本来完成此功能。
截至目前,我能够检测到正在运行的控制台并获取其文件路径。但是,我正在努力使用此信息来获取控制台的宽度。我已经尝试使用该命令tput,但我对 Linux/脚本还很陌生,因此我真的不知道该怎么做。
这样做的原因是我希望能够设置一个cron条目,每隔一段时间通知控制台其宽度/列。
到目前为止,这是我的代码:
#!/bin/bash
#Get PID of terminal
#terminal.txt holds most recent PID of console in use
value=$(</home/test/Documents/terminal.txt)
#Get tty using the PID from terminal.txt
TERMINAL="$(ps h -p $value -o tty)"
echo $TERMINAL
#Use tty to get full filepath for terminal in use
TERMINALPATH=/dev/$TERMINAL
echo $TERMINALPATH
COLUMNS=$(/home/test/Documents/get_columns.sh)
echo $COLUMNS
Run Code Online (Sandbox Code Playgroud)
#!/usr/bin/env bash
echo $(/usr/bin/tput cols)
Run Code Online (Sandbox Code Playgroud)
TERMINAL&的正常输出TERMINALPATH是and ,例如&pts/terminalnumber/dev/pts/terminalnumberpts/0/dev/pts/0
该命令tput有两个不同的选项来设置颜色setf和setaf,但它们在我的计算机上似乎都可以正常工作:
$ tput setf 2 && echo 'Hello world!'
$ tput setaf 2 && echo 'Hello world!'
Run Code Online (Sandbox Code Playgroud)
似乎有一个类似的两重性与setb和setab。
这两个选项有什么区别?
tput civis
Run Code Online (Sandbox Code Playgroud)
成功隐藏光标。
tput cvvis
Run Code Online (Sandbox Code Playgroud)
应该取消隐藏它,但它没有。
知道可能是什么问题吗?
我想隐藏光标,我知道tput命令。我确实搜索了它的手册页。在网上搜索,我发现
$ tput civis # to hide the cursor
$ tput cnorm # to bring back the cursor
Run Code Online (Sandbox Code Playgroud)
这些工作完美,但这些选项在手册页的任何地方都没有提到。
他们在哪里正式记录?
在.*rc我在网上或各种代码中看到的人们的 ' ' 文件中,我倾向于看到很多人手动使用 ANSI 转义序列而不是使用tput.
我有tput更普遍/更安全的理解,所以这让我想知道:
是否有任何客观原因应该使用转义序列来代替tput?(便携性、错误鲁棒性、异常终端......?)
有没有办法清除终端,而不是将提示留在屏幕顶部,而是将其留在中间?看起来clear基本上忽略了所有命令行参数。
我以为会有一些方法可以做到这一点,tput但找不到。
我创建了这个函数,它打印输出,如示例图像中所示。但是,这个功能的实现似乎太复杂了。
有什么方法可以改进它或实施替代解决方案吗?

#!/bin/bash
function box_out() {
input_char=$(echo "$@" | wc -c)
line=$(for i in `seq 0 $input_char`; do printf "-"; done)
# tput This should be the best option. what tput does is it will
# read the terminal info and render the correctly escaped ANSI code
# for you.
# Code like \033[31m will break the readline library in some of the
# terminals.
tput bold
line="$(tput setaf 3)${line}"
space=${line//-/ }
echo " ${line}"
printf '|' ; echo -n …Run Code Online (Sandbox Code Playgroud) 如何打印$myvar填充以使其位于终端的中心,并且任一侧都=位于屏幕的边缘?
我在 bash 脚本中有一个函数:message_offset它用于打印 bash 脚本的状态。
即你会调用它向它传递一条消息和一个状态,就像这样
message_offset "install font library" "[ OK ]"
Run Code Online (Sandbox Code Playgroud)
它会打印到终端,其中printf的%*s格式用于始终将最右边的字符设置[ OK ]为 80 列宽,例如输出将是
install font library [ OK ]
update configuration file on server [ ERR ]
^
|
always
at 80
Run Code Online (Sandbox Code Playgroud)
如果echo使用输出看起来像这样
install font library [ OK ]
update configuration file on server [ ERR ]
Run Code Online (Sandbox Code Playgroud)
代码:
#!/usr/bin/env bash
function message_offset() {
local message="$1"
local status="$2"
# compensate for the message length by reducing …Run Code Online (Sandbox Code Playgroud)