8

I want to add the following line:

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

to the end of the file /etc/rc.d/rc.local if it does not already exist.

How can I do that from linux command line? I assume grep or sed would work, but I am not familiar enough with either to get it to work. Right now I use echo, but that just keeps adding it over and over again.

cHam
  • 2,624
  • 7
  • 26
  • 28

2 Answers2

15

Assuming you want it at the end of the file:

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"
ahilsend
  • 931
  • 6
  • 15
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.

Kent
  • 189,393
  • 32
  • 233
  • 301