原先比较愚蠢的做法:

grep "include /etc/cinder/volumes/*" /etc/tgt/targets.conf || echo 'include /etc/cinder/volumes/*' | cat - /etc/tgt/targets.conf > /etc/tgt/targets.conf

后来,直接用sed处理:

grep "include /etc/cinder/volumes/*" /etc/tgt/targets.conf || sed -i '1i include /etc/cinder/volumes/*' /etc/tgt/targets.conf
  1. 其中i表示插入,而且是添加到当前行的上一行;而a是添加到当前行的后一行。

  2. 注意:在sed中没有第0行之说。

blog.51cto.com 标签: 字符串操作, sed, cat, 插入字符串