我想打印一个man 风格的使用消息来描述一个像这样输出的 shell 函数man find
:
NAME
find - search for files in a directory hierarchy
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
DESCRIPTION
This manual page documents the GNU version of find. GNU find searches the directory tree rooted at each
given starting-point by evaluating the given expression from left to right, according to the rules of
precedence (see section OPERATORS), until the outcome is known (the left hand side is false for …
Run Code Online (Sandbox Code Playgroud) 我有一个 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
。
编辑:
进一步阅读之前的重要说明:由于Kusalananda 的 anwser,我的问题现在毫无用处,因为他指出了我使用的另一个脚本中的错误。现在此错误已修复,不再出现我描述的以下错误!
您的代码中有两个问题导致 $extension 中的值被扩展
第二个问题是函数本身
TL; 博士
当我开始写这个问题时,我被卡住了,但找到了一个有效的解决方案(使用shell noglob),但我仍然有一个“是否可以写”的问题。
我确定这已经得到了回答,但我在数百个关于 bash 扩展的主题中迷失了方向。
这是我的情况:
ffmpeg
使用-pattern_type glob -i
接受正则表达式的选项模式*.JPG
)*.JPG
)$extension
)$1
)$extension
默认情况下扩展为类似的模式*.JPG
请注意,所有这些操作都在脚本中进行script.sh
。
因此,用户可以运行:
script.sh 'JPG' ; # where $1='JPG'
Run Code Online (Sandbox Code Playgroud)
注意:在script.sh
,我连接*.
并$1
获得更最终的正则表达式,如*.JPG
我读到(从这里/sf/answers/801954751/)外壳扩展发生在ffmpeg
接收参数之前(ffmpeg
甚至不知道外壳扩展发生)
我试图以${extension}
不同的方式使用引号、双引号或反斜杠,但没有成功。
以下是一些尝试:
ffmpeg ... "${extension}"
Run Code Online (Sandbox Code Playgroud)
导致:(ffmpeg …