add line to a file ONLY if it is not in file already

grep, linux, sed

Solution

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"

Problem

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.

Original source

Related problems