小编Fel*_*rez的帖子

Grep 单词边界

根据 GNU 文档:

‘\<’ Match the empty string at the beginning of word.
‘\>’ Match the empty string at the end of word.
Run Code Online (Sandbox Code Playgroud)

我的 /etc/fstab 看起来像这样:

/dev/sdb1       /media/fresh      ext2   defaults     0 0
Run Code Online (Sandbox Code Playgroud)

我希望 grep 为 /media/fresh 的存在返回 TRUE/FALSE。我尝试使用\<\>但没有用。为什么?

egrep '\</media/fresh\>' /etc/fstab
Run Code Online (Sandbox Code Playgroud)

解决方法:

egrep '[[:blank:]]/media/fresh[[:blank:]]' /etc/fstab
Run Code Online (Sandbox Code Playgroud)

但它看起来更丑。

我的 grep 是 2.5.1

grep regular-expression

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

运行命令,然后在一行中执行参数替换

bash# hostname
host1.example.com
Run Code Online (Sandbox Code Playgroud)

我只想要host1。所以:

SHORT_HOST=$(/bin/hostname)
SHORT_HOST=${SHORT_HOST%%.*}
Run Code Online (Sandbox Code Playgroud)

我可以把它变成单衬吗?- 或者 - 使 $SHORT_HOST 只读但仍获得短主机名的最佳方法是什么?

bash command-substitution

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

Bash 扩展通配符

我无法从ls使用 bash 扩展通配符的输出中仅显示一个文件。

info bash

  If the `extglob' shell option is enabled using the `shopt' builtin,
several extended pattern matching operators are recognized.  In the
following description, a PATTERN-LIST is a list of one or more patterns
separated by a `|'.  Composite patterns may be formed using one or more
of the following sub-patterns:

`?(PATTERN-LIST)'
     Matches zero or one occurrence of the given patterns.

`*(PATTERN-LIST)'
     Matches zero or more occurrences of the given patterns.

`+(PATTERN-LIST)'
     Matches one …
Run Code Online (Sandbox Code Playgroud)

bash wildcards

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