LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1264|回复: 2

if ! grep "^$id$" $file >/dev/null 2>&

[复制链接]
发表于 2009-12-25 02:54:38 | 显示全部楼层 |阅读模式
如题:部分代码如下。

id="`cat $home/.ssh/identity.pub`"
file=$home/.ssh/authorized_keys
if ! grep "^$id$" $file >/dev/null 2>&1 ; then
echo adding id to ssh file $file
echo $id >> $file
fi

我知道 >/dev/null 2>&1的意思

>/dev/null 不打印信息

2>&1  错误和输出合并

但是 grep "^$id$" $file 这是什么意思呢?
发表于 2009-12-25 10:21:55 | 显示全部楼层
只包含 $id 的行
回复 支持 反对

使用道具 举报

发表于 2009-12-25 16:13:57 | 显示全部楼层
$id是变量,"`cat $home/.ssh/identity.pub`" ,应该是cat $home/.ssh/identity.pub显示的内容
$file是一个变量,指的是$home/.ssh/authorized_keys

"^$id$"里的^是行开头,$表示行结尾。如"^file$"则匹配只有字符串file的行。这里$id也会进行相应的变量替换,替换为之前的"`cat $home/.ssh/identity.pub`" 的内容。

再举个例子:
grep "^string$" file 表示显示文件file里匹配行内只有字符串string的行。

如果 grep "^$id$" $file >/dev/null 2>&1 返回值,则为真,否则为假
!为取反。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表