小编zet*_*tah的帖子

如何检查管道是否为空并对数据运行命令(如果不是)?

我在 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

测试上述管道是否产生输出的标准/规范方式不同需要保留输入以将其传递给程序。这概括了如何将输出从一个进程传送到另一个进程,但仅在第一个进程有输出时才执行?专注于发送电子邮件。

shell bash pipe

73
推荐指数
7
解决办法
8万
查看次数

diff - 输出行号

我想使用 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)

diff

25
推荐指数
1
解决办法
4万
查看次数

使用 `dd` 剪切文件结尾部分

可能有一个简单的技巧可以做到这一点,但我无法从手册页中弄清楚。

例如,如何使用dd?

dd

20
推荐指数
2
解决办法
3万
查看次数

任何用于检查 GTK3 元素的工具?

gtkparasite非常适合gtk+2,但不幸的是它不适用于gtk+3.

有什么替代方法吗?

gui gtk3

11
推荐指数
2
解决办法
3060
查看次数

生成不带换行符的 grep 输出

请考虑这个片段:

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它和类似的并发症?

grep bash

10
推荐指数
2
解决办法
3万
查看次数

hunspell:从命令行将单词添加到字典中

hunspell 手册页

...
    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 读取的兼容词典的“通用”程序?

spell-checking hunspell

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

从内联命令检查文件名是否存在

我有一个名为 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)

那么为什么会这样,或者我该如何解决这个问题?

bash

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

标签 统计

bash ×3

dd ×1

diff ×1

grep ×1

gtk3 ×1

gui ×1

hunspell ×1

pipe ×1

shell ×1

spell-checking ×1