Using sed: it will be inserted at the end of the line. You can also pass variables, as usual, of course.
grep -qxF "port=9033" $light.conf if [ $? -ne 0 ]; then sed -i "$ a port=9033" $light.conf else echo "port=9033 already added" fi
Using oneliner sed
grep -qxF "port=9033" $lightconf || sed -i "$ a port=9033" $lightconf
Using echo may not work as root, but it will work like that. But this will not allow you to automate the process if you want to do this, because it can request a password.
I had a problem when I tried to edit from the root for a specific user. Just adding $username used to be a fix for me.
grep -qxF "port=9033" light.conf if [ $? -ne 0 ]; then sudo -u $user_name echo "port=9033" >> light.conf else echo "already there" fi
Rakib Fiha Feb 27 '19 at 5:57 2019-02-27 05:57
source share