You are not logged in.

#1 2010-05-30 19:39:11

robrene
Member
Registered: 2009-04-16
Posts: 168

[bash] Problem with @ symbol in argument when using notify-send

#!/bin/bash
#
# Custom event script for mcabber
# Plays sounds and shows notifications
#
# robrene
#

# Commands
PLAY="/usr/bin/play -v 0.3 $HOME/.mcabber/message.wav"
NOTIFY="/usr/bin/notify-send -c im.received"

# Username to look for in MUC messages
username=$USER

# Arguments
event=$1
arg1=$2
arg2=$3
filename=$4

echo $event $arg1 $arg2 $filename >> ~/event.txt

if [ $event = "MSG" ]; then
    # $arg1 is the type of message
    # $arg2 is where the message came from
    case "$arg1" in
    IN)
        # Play sound
        $PLAY > /dev/null 2>&1
        # Process file (show notification)
        if [ -f "$filename" ]; then
            $NOTIFY "$arg2" "`cat $filename`"
            rm $filename
        fi
        ;;
    MUC)
        # Process file
        filename=$4
        if [ -f "$filename" ]; then
            # Find mentioning of user
            if grep -i $username $filename; then
                # Play sound
                $PLAY > /dev/null 2>&1
                # Show notification
                $NOTIFY "$arg2" "`cat $filename`"
            fi
            rm $filename
        fi
        ;;
    OUT)
        ;;
    esac
fi

I'm writing a script for mcabber that is supposed to make a sound and show a notification under certain circumstances. The problem is that the argument that provides the username of the sender of the message ($arg2) always contains an @ symbol. For example alice@jabber.org.

Now, when I run notify-send foo@bar "message body", the notification doesn't show. If I run notify-send "foo@bar" "message body" there is no problem (note the quotes around the title).

As you see, I have quotes around $arg2 in my script. However, the notification still doesn't show! It does show if I run it by hand and provide it with a name that doesn't have an @ symbol, so I'm sure that's the problem. How do I fix this?


smile neutral sad big_smile yikes wink hmm tongue lol mad roll cool

Offline

#2 2010-05-30 21:25:29

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [bash] Problem with @ symbol in argument when using notify-send

How 'bout \@ or some other escape?

Offline

#3 2010-05-30 21:32:30

dieghen89
Member
From: Italy
Registered: 2009-06-24
Posts: 134

Re: [bash] Problem with @ symbol in argument when using notify-send

Try also with ' instead of " smile


XPS 13 DE 2015 + K*5
"Machines are so stupid that if you tell them to do something perfect, they'll do it"

Offline

#4 2010-05-30 22:21:03

robrene
Member
Registered: 2009-04-16
Posts: 168

Re: [bash] Problem with @ symbol in argument when using notify-send

Alright, so apparently there was some totally different problem. I tested and tested, and found out that I needed to add > /dev/null 2>&1 to it in order to work. I'm guessing mcabber does something funky otherwise... Anyhow, it all works now. here is the code for anyone interested:

#!/bin/bash
#
# Custom event script for mcabber
# Plays sounds and shows notifications
#
# robrene
#

# Commands
PLAY="/usr/bin/play -v 0.3 $HOME/.mcabber/message.wav"
NOTIFY="/usr/bin/notify-send -c im.received"

# Username to look for in MUC messages
username=$USER

# Arguments
event=$1
arg1=$2
arg2=$3
filename=$4

# Do something
if [ $event = "MSG" ]; then
    # $arg1 is the type of message
    # $arg2 is where the message came from
    case $arg1 in
    IN)
        # Play sound
        $PLAY > /dev/null 2>&1
        # Process file (show notification)
        if [ -f $filename ]; then
            $NOTIFY "$arg2" "`cat $filename`" > /dev/null 2>&1
            rm $filename
        fi
        ;;
    MUC)
        # Process file
        if [ -f $filename ]; then
            # Find mentioning of user
            if grep -i $username $filename; then
                # Play sound
                $PLAY > /dev/null 2>&1
                # Show notification
                $NOTIFY "$arg2" "`cat $filename`"
            fi
            rm $filename
        fi
        ;;
    OUT)
        ;;
    esac
fi

Last edited by robrene (2010-05-30 22:21:51)


smile neutral sad big_smile yikes wink hmm tongue lol mad roll cool

Offline

Board footer

Powered by FluxBB