你如何将结果放在一条线上? (在bash中)

时间:2014-03-12 21:02:18

标签: bash colors rows col

所以我创建了这个文件,我目前在一个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

3 个答案:

答案 0 :(得分:0)

尝试less -rless -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.

答案 1 :(得分:0)

我的理解,你需要这样的东西:

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

答案 2 :(得分:0)

命令echo,自动添加新行。 如果要在一行中打印输出,请使用printf命令。

要更改颜色,您可以使用\ e,如下所示:

echo -e "\e0;31m Your text"

此行将以红色打印您的文字。 您可以更改号码31。