我在 bash 脚本中输入了一行,并想在将它提供给程序之前检查管道是否有数据。
搜索我找到了,test -t 0
但在这里不起作用。总是返回假。那么如何确定管道有数据呢?
例子:
echo "string" | [ -t 0 ] && echo "empty" || echo "fill"
Run Code Online (Sandbox Code Playgroud)
输出: fill
echo "string" | tail -n+2 | [ -t 0 ] && echo "empty" || echo "fill"
Run Code Online (Sandbox Code Playgroud)
输出: fill
与测试上述管道是否产生输出的标准/规范方式不同?需要保留输入以将其传递给程序。这概括了如何将输出从一个进程传送到另一个进程,但仅在第一个进程有输出时才执行?专注于发送电子邮件。
我想使用 cli 工具进行文件比较,并且在输出行之前需要行号,这有助于我可以跳转到行差异,因为我使用的工具可以了解跳转的位置,如果行是这样开始的 :line-number: regular line contents
所以我尝试了diff
,阅读文档似乎是可能的:
-D, --ifdef=NAME output merged file with `#ifdef NAME' diffs
--GTYPE-group-format=GFMT format GTYPE input groups with GFMT
--line-format=LFMT format all input lines with LFMT
--LTYPE-line-format=LFMT format LTYPE input lines with LFMT
These format options provide fine-grained control over the output
of diff, generalizing -D/--ifdef.
LTYPE is `old', `new', or `unchanged'. GTYPE is LTYPE or `changed'.
GFMT (only) may contain:
%< lines from FILE1
%> lines from FILE2
%= lines common to …
Run Code Online (Sandbox Code Playgroud) 请考虑这个片段:
X=$(grep -m1 'some-pattern' some-file | sed -n 's/.* //p')
如果某些模式条件与任意文本文件中的行匹配,我想将最后一个单词放入变量中
我的问题是变量最后X
有 CR 或 LF 或 CRLF,具体取决于我想删除的源文件,因为它会干扰我打算执行的后续操作。
我什至尝试过类似的事情:
X=$(grep -m1 'some-pattern' some-file | sed -n 's/.* \([A-Za-z]\+\)/\1/p')
因此期望sed
输出受到限制,[A-Za-z]+
但 X 变量中仍然存在这些令人讨厌的字节。
我怎样才能摆脱它,而无需使用像看到的是什么字节在结束了太多的代码,xxd
那么cut
它和类似的并发症?
...
When in the -a mode, hunspell will also accept lines of single
words prefixed with any of '*', '&', '@', '+', '-', '~', '#',
'!', '%', '`', or '^'. A line starting with '*' tells hunspell
to insert the word into the user's dictionary (similar to the I
command).
...
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的事情:echo "* my_word" | hunspell -a
但是这个词不在我的字典中,因为解析示例文件再次将其显示为拼写错误的词
这是如何工作的,如何添加自定义单词?
或者使用 Aspell,或者任何写入 Hunspell/Aspell 读取的兼容词典的“通用”程序?
我有一个名为 input.txt 的文件,其中包含以下内容:
...
文件“Edie - Realities.txt”TXT
...
我想读取它,然后从以 FILE 开头的行中删除文件名路径,并检查它是否存在,因此:
[ -f $(cat input.txt | grep FILE | grep -o "\".*\"") ] && echo "exist" || echo "does not exist"
但这输出:
[: too many arguments
does not exist
Run Code Online (Sandbox Code Playgroud)
如果我运行:
echo $(cat input.txt | grep FILE | grep -o "\".*\"")
Run Code Online (Sandbox Code Playgroud)
我得到了我的期望:
"Edie - Realities.txt"
Run Code Online (Sandbox Code Playgroud)
那么为什么会这样,或者我该如何解决这个问题?