Append string of conditions to file using sed

Viewed 131

I have to make a script which looks at /etc/gdm/Xsession and adds -a ! -x "$HOME/.xsession" to it if needed.

1) I want to check if the file already contains the added part. Using grep, I tried this:

if ! grep -q '-a ! -x "$HOME/.xsession"' "$xsessionScript"

It doesn't work as grep tries to interpret everything that's between the simple quotes.

2) If I find that the file wasn't modified, I want to append -a ! -x "$HOME/.xsession" to the if statement: if [ "$1" != "default" -a "$1" != "custom" ] so that it looks like:

if [ "$1" != "default" -a "$1" != "custom" -a ! -x "$HOME/.xsession" ]

I try doing it using sed:

sed -i.bck '/\"$1" != "default" -a "\$1" != "custom"/a  -a ! -x "$HOME/.xsession"'

but this will create a new line instead of adding the string after "custom".

Please help me!

Thanks a lot :)

Btw, I'm on CentOs 6.6.1, sed 4.2.1, bash 4.1.2

EDIT:

The file I want to edit looks like this:

[...]
case $# in
    [...]
    case "$1" in
    [...]
    *)
        if [ "$1" != "default" -a "$1" != "custom" ]; then # I want this line to become:
====> # if [ "$1" != "default" -a "$1" != "custom" -a ! -x "$HOME/.xsession" ]
        exec $CK_XINIT_SESSION $SSH_AGENT /bin/sh -c "exec -l $SHELL -c \"$1\""
        fi
        ;;
    esac
esac
fi
[...]
2 Answers
Related