1

In order to avoid a conflict between two scripts, I want to change any value in one line of the file ~/.ideskrc with a zero. This is the line with the desired value:

Background.Delay: 0

I have a manual solution that handles the problem for now:

STRING="Background.Delay: 0"
if ! grep -q "$STRING" $HOME/.ideskrc;
then
    yad --geometry 300x300 --text="Rotate Backgrounds and Separate Backgrounds can not be run at the same time. \n
    Run Rotate Backgrounds and set that Delay to zero."
exit 0
fi

It would be nice to just change it and not require user intervention. I have tried a number of variations using sed without success. The most recent example:

sed -i -r 's/Background.Delay:[[:space:]]+1/Background.Delay: 0/' ~/.ideskrc

I'm only a basic-level script writer, so definitely could use some help. Thanks.

Final version now with yad formatting and revised message for anyone interested:

if ! grep -q "$STRING" $HOME/.ideskrc;
then
        sed -i 's/^\([[:blank:]]*Background\.Delay:\).*/\1 0/' "$HOME/.ideskrc"
yad --timeout=3 --no-buttons --geometry 500x100 --text-align=center --text="
<b>Rotate Backgrounds has been disabled to avoid conflict.</b>
"
fi

1 Answers1

1

Assuming that you want to delete the value after : on the line and insert a 0 there, unconditionally, and also assuming that the string Background.Delay: occurs at the very start of the line:

sed -i 's/^\([[:blank:]]*Background\.Delay:\).*/\1 0/' "$HOME/.ideskrc"

or

sed -i '/^[[:blank:]]*Background\.Delay:/ s/:.*/: 0/' "$HOME/.ideskrc"

This looks for the literal string Background.Delay: at the start of any line in the file .ideskrc in your home directory, possibly preceded by any number of tabs or spaces (these will be preserved), removes whatever comes after the : and inserts 0 after a space.

The changes are made in-place if your sed implementation supports using the -i option as I've shown above (see How can I achieve portability with sed -i (in-place editing)?).

Kusalananda
  • 333,661
  • Hmmm. Neither one appears to change the value in that line. For reference, here is the entire file: – Jerry Bond May 01 '21 at 12:05
  • @JerryBond If you have clarification to your issue, consider editing your question (rather than adding information in comments). – Kusalananda May 01 '21 at 12:10
  • OK: that (revised?) first command works! Thanks for your help. – Jerry Bond May 01 '21 at 12:17
  • @JerryBond Good! That means that the text on the line was indented with spaces or tabs (it wasn't clear from the question that it was). Both commands should ork now. You use the one you understand or that you think looks best. – Kusalananda May 01 '21 at 12:18