0

给定以下代码:

for u in `cat /tmp/expiring` ; do
  /var/tmp/pwx $u egrep [12345] >> /tmp/reallyexpiring
done

来自 /tmp/expiring 的 for 循环的输入如下所示:

bjones password good 25 to expire
rsmith password good 3 to expire

/tmp/expiring 将有几行,所有行都包含有关帐户到期前多少天的报告,并且每行有一个用户。

稍后在脚本中调用了一个 mailx 命令来发送电子邮件,但只发送给那些密码将在 1 到 7 天后到期的人。

问题是 grep 语句匹配 25 天和高于 7 天阈值的其他值。

关于如何修改脚本以仅匹配 1 2 3 4 5 6 7 的离散值的任何想法?例如,25 匹配,因为数字 25 中的 2。

4

1 回答 1

1

如果您确定您的号码前后会有一个空格(或另一个固定字符),您可以这样做

grep " [1-7] "

在你的情况下,

/var/tmp/pwx $u grep " [1-7] " >> /tmp/reallyexpiring

测试

$ cat a
this is 2 and
this is 23 ho
but this is 8
and this 7 is seven

$ grep " [0-7] " a
this is 2 and
and this 7 is seven
于 2013-10-18T13:12:57.190 回答