Add the line to the file ONLY if it is not already in the file

I want to add the following line:

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

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

How can I do this from linux command line? I assume grep or sed will work, but I'm not familiar enough with getting it to work. I'm using echo right now, but that just adds it over and over.

+6
source share
2 answers

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" 
+12
source

one parameter consists of two steps:

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

also possible to do with awk,

save all the lines in the array, while saving, if the line was found, exit. otherwise, add the line to the END{} block to the desired location.

PS You did not indicate in the file where to add this line.

+2
source

Source: https://habr.com/ru/post/949827/


All Articles