ludvick
ludvick

Reputation: 11

a bash script with a yad window and checking internet connection

I'm working on a bash script, opening a yad window and keeping it opened until internet connection is present and then closing it.

It looks as follow:

#!/bin/bash

yad_wind1_id=""
yad_wind1_id_txt_str=""
NET_STATE=0

function test_net_connection()
{
    (ping -c 1 www.google.com &>/dev/null)

    XCODE=$?

    if [ $XCODE == 0 ]; then
        NET_STATE=1
    else
        NET_STATE=0
    fi
}


function kill_yad_proc()
{

     kill_proc=$(ps aux | grep "$1" | grep -v "grep $1" | \
     awk '{print $2}' | sed 's:^:kill :')

     eval $kill_proc

}

function yad_proc_id_string()
{

        yad_win_id=`echo $(date +%F-%H-%M-%6N) | \
        sed 's/-/_/g' | sed 's/^/text=/'`

        yad_win_id_txt_str="--""$yad_win_id"

}

yad_proc_id_string

# ...and here starts my problem...

while [[ $NET_STATE == 0 ]]; do

    test_net_connection

    if [[ $XCODE == 0 ]]; then
            NET_STATE=1
            kill_yad_proc "$yad_win_id"
            break
            exit
    else
            NET_STATE=0
            continue
    fi

done | `yad --fixed --skip-taskbar --undecorated \
    "$yad_win_id_txt_str" \
    --text="Waiting for internet connection..." \
    --button='Quit:bash -c "kill -USR1 $YAD_PID"' \
    --image=disconnected.png`

In this version script works just partly: when internet is off, yad window appears and disappears when internet is on (ping returns 0). Unfortunately, when I press [Quit] button or [escape] (however I'd like to keep this option), it doesn't work as I'd like it to.

What should I add/change? Where the mistake is done?

Upvotes: 0

Views: 738

Answers (1)

guest
guest

Reputation: 1

"when I press [Quit] button or [escape] (however I'd like to keep this option)" I guess yad is outside loop and this is your problem.

TIP. - I do not see sense to check the connection with ping. But maybe you need and maybe this is best way. Best should be find file inside system for internet conection status and use inotifywait however inotify sometimes is also buggy so you should be carefully. - You can add own icon tray with yad, but you need create two functions with yad with other icons and title. When inetrnet status will change, you can kill first yad window and open next window. This can be in "while" loop when status internet is tested.

Upvotes: 0

Related Questions