8

我想添加以下行:

nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &

/etc/rc.d/rc.local如果文件不存在,则到文件末尾。

我怎样才能从linux命令行做到这一点?我假设grepsed会工作,但我对任何一个都不够熟悉,无法让它工作。现在我使用echo,但这只是一遍又一遍地添加它。

4

2 回答 2

15

假设您希望它在文件末尾:

LINE="nohup java -jar /mnt/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &"
FILE=/etc/rc.d/rc.local
grep -q "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
于 2013-07-19T14:07:15.160 回答
2

one option is two steps:

grep -q "yourline" /path/file||sed -i '/..place../ a \the line' file

also possible to do with awk,

save all lines in array, during saving if the line was found, exit. otherwise, add the line in END{} block to the right place.

P.S. You didn't tell in the file, where to add that line.

于 2013-07-19T14:05:17.527 回答