所以我制作了这个文件,我目前正在一个 bash shell 中运行它,它列出得很好,但是每个结果都放在它自己的单独的行上。我想知道如何制作相同的程序,但让它横向放置结果,一个又一个结果。
x=0;while [ $x -le 256 ]
do
t=$(tput sgr 0);r=$(tput setaf $x);echo "${r} $x ${t}";((x=$x+1))
done
命令echo
,自动添加新行。如果要在一行中打印输出,请使用printf
Command。
要更改颜色,可以使用 \e,如下所示:
echo -e "\e0;31m Your text"
此行将以红色打印您的文本。您可以更改数字 31。
尝试less -r
或less -R
:
从less
手册页:
-r or --raw-control-chars
Causes "raw" control characters to be displayed. The default is to
display control characters using the caret notation; for example, a
control-A (octal 001) is displayed as "^A". Warning: when the -r
option is used, less cannot keep track of the actual appearance of the
screen (since this depends on how the screen responds to each type of
control character). Thus, various display problems may result, such as
long lines being split in the wrong place.
-R or --RAW-CONTROL-CHARS
Like -r, but tries to keep track of the screen appearance where possible.
This works only if the input consists of normal text and possibly some
ANSI "color" escape sequences, which are sequences of the form:
ESC [ ... m
where the "..." is zero or more characters other than "m". For the
purpose of keeping track of screen appearance, all control characters
and all ANSI color escape sequences are assumed to not move the cursor.
You can make less think that characters other than "m" can end ANSI
color escape sequences by setting the environment variable
LESSANSIENDCHARS to the list of characters which can end a color escape
sequence.
我怎么理解,你需要这样的东西:
x=0;while [ $x -le 256 ]
do
t=$(tput sgr 0);r=$(tput setaf $x);echo -n "${r} $x ${t}";((x=$x+1))
done
你忘了使用 echo -n (没有换行)
带列的格式化版本:
#!/bin/bash
t=$(tput sgr 0)
for x in {0..255}; do
printf "%s%4s${t}" "$(tput setaf $x)" $x
if [ $((x % 15)) -eq 0 ]; then echo; fi;
done