首先,关于颜色,此脚本可能会让您感兴趣。它在控制台中工作正常(在终端中,选项卡宽度设置不正确)。
#!/bin/bash
setterm -regtabs 4
Color_names="bla red gre yel blu mag cya whi"
Color_arr=($Color_names)
tput setaf 4
tput setab 7
echo -n " normal "
tput sgr0
echo -n " "
tput setaf 7
tput setab 4
echo " bright "
tput sgr0
for cmd in sgr0 bold
do
tput $cmd
for m in 0 1 2 3 4 5 6 7
do
tput setaf $m
echo -n ${Color_arr[$m]}" "
done
done
echo
tput sgr0
cat /sys/module/vt/parameters/default_red \
/sys/module/vt/parameters/default_grn \
/sys/module/vt/parameters/default_blu | sed s/,0/", "/g | \
sed s/^0/" "/g | \
tr "," "\t"
其次,使用ls不仅可以做更多的事情--color=auto
。但是,需要进行一些工作才能整理出所有内容。我编写了一个脚本,以方便理解,并以系统的方式为文件着色(基于扩展名)。
如果太长而无法粘贴,请在此处查看。
#!/bin/bash
# must be sourced
# see `man dircolors`
# in particular `dircolors -p > coldb.txt` (-p, --print-database)
# attributes
# 00 = none
# 01 = bold (bright)
# 04 = underscore (white; disables color code)
# 05 = blink (gray background)
# 07 = reverse (color as background)
# 08 = concealed (doesn't do anything)
# colors
R='00;31' # red
G='00;32' # green
Y='00;33' # yellow (brown in my console)
B='00;34' # blue
M='00;35' # magenta
C='00;36' # cyan
W='00;37' # white
# bold (bright)
BR='01;31' # red
BG='01;32' # green
BY='01;33' # yellow
BB='01;34' # blue
BM='01;35' # magenta
BC='01;36' # cyan
BW='01;37' # white
# for di (below) and possible some others
BL='00;30' # black
GR='01;30' # gray
# background
BGBL='40' # blue
BGRE='41' # red
BGGR='42' # green
BGYE='43' # yellow
BGBL='44' # blue
BGMA='45' # magenta
BGCY='46' # cyan
BGWH='47' # white
RS="rs=0" # reset
DI="di=$BB" # directory
LN="ln=$C" # link
MH="mh=00" # multihardlink (file with more than one link)
PI="pi=$BY;$BGBL" # pipe (FIFO)
SO="so=$BM" # socket
DO="do=$BM" # door
BD="bd=$BY;$GBBL" # block device driver
CD="cd=$BY;$BGBL" # character device driver
OR="or=$BR" # symlink to nonexistent (or non-stat'able) file
SU="su=$W;$BGRE" # file that is setuid (u+s)
SG="sg=$BL;$BGYE" # file that is setgid (g+s)
CA="ca=$BL;$BGRE" # file with capability
TW="tw=$BL;$BGGR" # dir that is sticky and other-writable (+t,o+w)
OW="ow=$BB;$BGGR" # dir that is other-writable (o+w) and not sticky
ST="st=$BW;$BGBL" # sticky bit dir (+t) and not other-writable
EX="ex=$BG" # file with execute permission
ARCHIVE="*.tar=$BR:*.tgz=$BR:*.arj=$BR:*.taz=$BR:*.lzh=$BR:*.lzma=$BR:*.tlz=$BR:*.txz=$BR:*.zip=$BR:*.z=$BR:*.Z=$BR:*.dz=$BR:*.gz=$BR:*.lz=$BR:*.xz=$BR:*.bz2=$BR:*.bz=$BR:*.tbz=$BR:*.tbz2=$BR:*.tz=$BR:*.deb=$BR:*.rpm=$BR:*.jar=$BR:*.war=$BR:*.ear=$BR:*.sar=$BR:*.rar=$BR:*.ace=$BR:*.zoo=$BR:*.cpio=$BR:*.7z=$BR:*.rz=$BR"
IMAGE="*.jpg=$BM:*.jpeg=$BM:*.gif=$BM:*.bmp=$BM:*.pbm=$BM:*.pgm=$BM:*.ppm=$BM:*.tga=$BM:*.xbm=$BM:*.xpm=$BM:*.tif=$BM:*.tiff=$BM:*.png=$BM:*.svg=$BM:*.svgz=$BM:*.mng=$BM:*.pcx=$BM"
MOVIE="*.mov=$M:*.mpg=$M:*.mpeg=$M:*.m2v=$M:*.mkv=$M:*.webm=$M:*.ogm=$M:*.mp4=$M:*.m4v=$M:*.mp4v=$M:*.vob=$M:*.qt=$M:*.nuv=$M:*.wmv=$M:*.asf=$M:*.rm=$M:*.rmvb=$M:*.flc=$M:*.avi=$M:*.fli=$M:*.flv=$M:*.gl=$M:*.dl=$M:*.xcf=$M:*.xwd=$M:*.yuv=$M:*.cgm=$M:*.emf=$M:*.axv=$M:*.anx=$M:*.ogv=$M:*.ogx=$M"
AUDIO="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