Appending a line to a file only if it does not already exist

后端 未结 10 1237
深忆病人
深忆病人 2020-11-30 16:44

I need to add the following line to the end of a config file:

include \"/configs/projectname.conf\"

to a file called lighttpd.conf

10条回答
  •  不知归路
    2020-11-30 17:15

    If, one day, someone else have to deal with this code as "legacy code", then that person will be grateful if you write a less exoteric code, such as

    grep -q -F 'include "/configs/projectname.conf"' lighttpd.conf
    if [ $? -ne 0 ]; then
      echo 'include "/configs/projectname.conf"' >> lighttpd.conf
    fi
    

提交回复
热议问题