相关疑难解决方法(0)

保存到文件的差异补丁输出。如何查看它的语法颜色?

我有一个包含文本文件的子文件夹的文件夹。

我时不时地运行一个脚本来制作该文件夹树的副本,然后创建一个包含新信息的新文件夹。

之后,有了旧版本和新版本,我运行 diff 以了解旧版本和新版本之间发生了什么变化,将输出发送到文件:

diff myFiler.old myFolder > diff_report.txt
Run Code Online (Sandbox Code Playgroud)

问题是。

- 如何查看 diff_report.txt 语法的颜色?

我并不是要像 git 使用 diff 时那样查看实际输出的颜色,而是使用这种语法着色来查看已保存到文件中的 diff 输出。

colors diff syntax-highlighting

5
推荐指数
1
解决办法
3449
查看次数

将脚本输出重定向到文件时防止 tput 转义序列

我有一个 shell 函数,它使用tput.

color=$( tput setaf 1 )
normal=$( tput sgr0 )
colorize() {
    echo "${color}$1$(normal)"
}
Run Code Online (Sandbox Code Playgroud)

当我调用该函数时,终端按预期显示彩色文件名。

$ myScript.sh .
/path/to/dir # (in color red)
Run Code Online (Sandbox Code Playgroud)

然后,如果我将输出重定向到一个文件:

$ myScript.sh > /tmp/file.log
Run Code Online (Sandbox Code Playgroud)

file.log 仍然包含转义序列,如:

^[[36m~/path/to/my/file/^[(B^[[[m
Run Code Online (Sandbox Code Playgroud)

难道这是关系到TERM和infocomp,并解释如何逃生末端序列?

这个想法是模拟一个“非显示”终端,不是吗?

我的术语是(Ubuntu 16.04):

$ echo $TERM
xterm
Run Code Online (Sandbox Code Playgroud)

tput当我的脚本被重定向到一个文件时,我应该怎么做来防止这种转义序列?

解决方法:手动给我的脚本添加一个选项来禁用着色,如ls或grep类似--color=none。

colors terminal io-redirection xterm tput

3
推荐指数
1
解决办法
1005
查看次数

如何更改ls中不同文件的颜色

所以我知道有一种方法可以更改目录、常规文件、bash 脚本等的文本颜色。有没有办法根据_file extension_?

例子:

$ ls -l
foo.txt  [is red] 
foo.text  [is blue] 
foo.secret  [is green] 
foo.txt  [is red]
Run Code Online (Sandbox Code Playgroud)

ls shell bash

1
推荐指数
1
解决办法
4764
查看次数

标签 统计

colors ×2

bash ×1

diff ×1

io-redirection ×1

ls ×1

shell ×1

syntax-highlighting ×1

terminal ×1

tput ×1

xterm ×1