原先比较愚蠢的做法:
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
其中i表示插入,而且是添加到当前行的上一行;而a是添加到当前行的后一行。
注意:在sed中没有第0行之说。