YAD Startup Script

interpretive language scripts


Moderator: Forum moderators

Post Reply
User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

YAD Startup Script

Post by ripwardog »

A small script that I use as a startup to welcome me and let me know if there is or isn't internet connectivity.

Code: Select all

#!/bin/bash

# May be reduced from default of 18 seconds to 15 or less if using an SSD or fast HD.
# Also if using a static IP rather than DHCP.
# I've used 10 here, but it seems long to me in every pup I've used it on.
sleep 10 

# Establish some string variables
# Change "Earthling" to your name or use the $USER system variable if you like.
# Example _yourname="John Doe" or _yourname=$USER
_yourname="Earthling"
_diamsg=""
_tb="Testing Connectivity"
_day="Today is `date +%A`"

# Establish some numerical variables
_hour=`date +%H`
_ret=252

if [ $_hour -ge 00 -a $_hour -le 11 ]
	then
		_tod="Good morning, "
	elif [ $_hour -ge 12 -a $_hour -le 17 ]
	then
		_tod="Good afternoon, "
	else
		_tod="Good evening, "
fi

# Concat the strings less internet status
_diamsg=$_tod"$_yourname"". "$_day" and..."

# Test if internet is active and connected
wget -q --spider http://google.com; _Ucon=$?

if [ $_Ucon -eq 0 ]
	then
		_diamsg=$_diamsg"   Internet access is confirmed.   "
	else 
		_diamsg=$_diamsg"---   WARNING: No Internet Connection   ---"
fi

# Loop until the Awesome button has been selected
	while [ $_ret -ne 0 ]
	do
# Open dialog
		yad 	--width=600 --center --fixed --on-top \
			--image="dialog-exclaimation" \
			--buttons-layout=center \
			--button="Awesome:0" \
			--text-align=center --title="$_tb" \
			--text-align=center --text="$_diamsg" \ 
            
		_ret=$?
	done
exit

After I made this screen shot I realized that $? would always be 0 and inform me that there was internet, whether or not there actually was internet connectivity.
I've corrected the script to concat the strings BEFORE the internet check to avoid the issue. Code SHOULD be correct now.
I've also discovered that the code I was using was NOT the proper way to test for internet connectivity. That has been corrected as well.
Turns out the way to determine if internet connectivity exists that is depicted in the screen shot is valid as long as netcat is installed, which it wasn't on my system.
Oh well. Live and learn.

Image

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: YAD Startup Script

Post by ripwardog »

I changed the script around a bit. Decided I didn't need a dialog if the internet was connected and instead called upon some speech synthesis.
The dialog only pops up now if the internet connection is down. I can hear if it (the internet) is up, some form of visual indicator helps when it's down.

Code: Select all

#!/bin/bash

# May be reduced from default of 18 seconds to 15 or less if using an SSD or fast HD.
# Also if using a static IP rather than DHCP.
# I've used 10 here, but it seems long to me in every pup I've used it on.
sleep 10 

# Establish some string variables
# Change "Earthling" to your name or use the $USER system variable if you like.
# Example _yourname="John Doe" or _yourname=$USER
_yourname="Earthling"
_diamsg=""
_tb="Testing Connectivity"
_day="Today is `date +%A`"

# Establish some numerical variables
_hour=`date +%H`
_ret=252

if [ $_hour -ge 00 -a $_hour -le 11 ]
	then
		_tod="Good morning, "
	elif [ $_hour -ge 12 -a $_hour -le 17 ]
	then
		_tod="Good afternoon, "
	else
		_tod="Good evening, "
fi

# Concat the strings less internet status
_diamsg=$_tod"$_yourname"". "$_day" and..."

# Test if internet is active and connected
wget -q --spider http://google.com; _Ucon=$?

if [ $_Ucon -eq 0 ]
	then
		_diamsg=$_diamsg"   Internet access is confirmed.   "
		trans -b -p -player mplayer en: :$lang "$_diamsg" >> NULL
		exit
	else 
		_diamsg=$_diamsg"---   WARNING: No Internet Connection   ---"
fi

#	Loop until the DAMN button has been pressed
	while [ $_ret -ne 0 ]
	do
#	Open dialog
	    yad --width=600 --center --fixed --on-top \
			--image="dialog-question" \
			--buttons-layout=center \
			--button="DAMN:0" \
			--text-align=center --title="$_tb" \
			--text-align=center --text="$_diamsg" \ 
            
		_ret=$?
	done
exit

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: YAD Startup Script

Post by ripwardog »

Did a bit more modding on this script. Now it checks to see if it can speak and if it can't it opens a dialog box.
Keep in mind, I'm just learning how to script and know there are many different ways to do almost every thing.

Code: Select all

#!/bin/bash

# Dialog Box Function
dialog_box (){
#	Loop until the DAMN button has been pressed
	while [ $_ret -ne 0 ]
	do
#	Open dialog
	    yad --width=600 --center --fixed --on-top \
			--image="dialog-question" \
			--buttons-layout=center \
			--button="$_bttn:0" \
			--text-align=center --title="$_tb" \
			--text-align=center --text="$_diamsg" \ 
            
		_ret=$?
	done
}

# Establish some string variables
# Change "Earthling" to your name or use the $USER system variable if you like.
# Example _yourname="John Doe" or _yourname=$USER
_yourname="Earthling"
_diamsg=""
_tb="Testing Connectivity"
_day="Today is `date +%A`"
_bttn="DAMN"

# Establish some numerical variables
_hour=`date +%H`
_ret=252

#check for installation of two commands? required for speech?
command -v trans > null;_transin=$?
command -v mplayer > null;_mplins=$?
#establish the go ahead speak variable
if [ $_transin == 0 ] && [ $_mplins == 0 ];then _canspeak=0;else _canspeak=1;fi

# May be reduced?
# I've used 10 here, but it seems long to me.
sleep 10 

if [ $_hour -ge 00 ] && [ $_hour -le 11 ]
	then
		_tod="Good morning, "
	elif [ $_hour -ge 12 ] && [ $_hour -le 17 ]
	then
		_tod="Good afternoon, "
	else
		_tod="Good evening, "
fi

# Concat the strings less internet status
_diamsg=$_tod"$_yourname"". "$_day" and..."

# Test if internet is active and connected
wget -q --spider http://google.com; _Ucon=$?

if [ $_Ucon == 0 ]
	then
		_bttn="AWESOME"
		_diamsg=$_diamsg"   Internet access is confirmed.   "
		if [ $_canspeak == 0 ]
			then
				trans -b -p -player mplayer en: :$lang "$_diamsg" > null
			else
				dialog_box
		fi
	else 
		_diamsg=$_diamsg"---   WARNING: No Internet Connection   ---"
		dialog_box
fi

https://x.com/SauronsMouth_DB/status/18 ... 6149732496

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

User avatar
bugnaw333
Posts: 310
Joined: Wed Jul 20, 2022 11:04 pm
Location: Cebu, Philippines
Has thanked: 492 times
Been thanked: 58 times

Re: YAD Startup Script

Post by bugnaw333 »

User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: YAD Startup Script

Post by ripwardog »

I'm aware. It's the inspiration for my shell script. I decided to delve into scripting to fill some time as I'm old and retired.

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: YAD Startup Script

Post by ripwardog »

I copied this script to my EasyOS startup folder only to find that 'mplayer' does not exist in the /usr/bin folder, but 'mpv' IS there so I altered the script just a bit more.

Code: Select all

#!/bin/bash

# Dialog Box Function
dialog_box (){
#	Loop until the DAMN button has been pressed
	while [ $_ret -ne 0 ]
	do
#	Open dialog
	    yad 	--width=600 --center --fixed --on-top \
			--image="dialog-question" \
			--buttons-layout=center \
			--button="$_bttn:0" \
			--text-align=center --title="$_tb" \
			--text-align=center --text="$_diamsg" \ 
            
		_ret=$?
	done
}

# Establish some string variables
# Change "Earthling" to your name or use the $USER system variable if you like.
# Example _yourname="John Doe" or _yourname=$USER
_yourname="Earthling"
_diamsg=""
_tb="Testing Connectivity"
_day="Today is `date +%A`"
_bttn="DAMN"

# some systems may not have mplayer installed, EasyOS for example.
#_player='mplayer'
_player='mpv'


# Establish some numerical variables
_hour=`date +%H`
_ret=252

#check for installation of two commands? required for speech?
command -v trans > null;_transin=$?
command -v $_player > null;_mplins=$?
#establish the go ahead speak variable
if [ $_transin == 0 ] && [ $_mplins == 0 ];then _canspeak=0;else _canspeak=1;fi

# May be reduced?
# I've used 10 here, but it seems long to me.
sleep 10 

if [ $_hour -ge 00 ] && [ $_hour -le 11 ]
	then
		_tod="Good morning, "
elif [ $_hour -ge 12 ] && [ $_hour -le 17 ]
	then
		_tod="Good afternoon, "
	else
		_tod="Good evening, "
fi

# Concat the strings less internet status
_diamsg=$_tod"$_yourname"". "$_day" and..."

# Test if internet is active and connected
wget -q --spider http://google.com; _Ucon=$?

if [ $_Ucon == 0 ]
	then
		_bttn="AWESOME"
		_diamsg=$_diamsg"   Internet access is confirmed.   "
		if [ $_canspeak == 0 ]
			then
				trans -b -p -player $_player en: :$lang "$_diamsg" > null
			else
				dialog_box
		fi
	else 
		_diamsg=$_diamsg"---   WARNING: No Internet Connection   ---"
		dialog_box
fi

Theoretically this one should work on all puppy derivations.

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: YAD Startup Script

Post by ripwardog »

I have now ran a check on whether or not mpv and/or mplayer is installed and recalibrated my script file.

Code: Select all

#!/bin/bash

# Dialog Box Function
dialog_box (){
#	Loop until the DAMN button has been pressed
	while [ $_ret -ne 0 ]
	do
#	Open dialog
	    yad --width=600 --center --fixed --on-top \
			--image="dialog-question" \
			--buttons-layout=center \
			--button="$_bttn:0" \
			--text-align=center --title="$_tb" \
			--text-align=center --text="$_diamsg" \ 
            
		_ret=$?
	done
}

# Establish some string variables
# Change "Earthling" to your name or use the $USER system variable if you like.
# Example _yourname="John Doe" or _yourname=$USER
_yourname="Earthling"
_diamsg=""
_tb="Testing Connectivity"
_day="Today is `date +%A`"
_bttn="DAMN"
_player="mpv"

# some systems may not have mplayer installed, EasyOS has mpv for example.
#_player='mplayer'
command -v $_player > /dev/null;_mplins=$?
if [ $_mplins != 0 ]
	then
		_canspeak=1
		_player="mplayer"
		command -v $_player > /dev/null;_mplins=$?
			if $_mplins != 0
				then _canspeak=1
				else _canspeak=0
			fi
	else _canspeak=0
fi

# Establish some numerical variables
_hour=`date +%H`
_ret=252

#check for installation of two commands? required for speech?
command -v trans > null;_transin=$?
#command -v $_player > null;_mplins=$?
#establish the go ahead speak variable
if [ $_transin == 0 ] && [ $_canspeak == 0 ];then _canspeak=0;else _canspeak=1;fi

# May be reduced?
# I've used 10 here, but it seems long to me.
sleep 3

if [ $_hour -ge 00 ] && [ $_hour -le 11 ]
	then
		_tod="Good morning, "
elif [ $_hour -ge 12 ] && [ $_hour -le 17 ]
	then
		_tod="Good afternoon, "
	else
		_tod="Good evening, "
fi

# Concat the strings less internet status
_diamsg=$_tod"$_yourname"". "$_day" and..."

# Test if internet is active and connected
wget -q --spider http://google.com; _Ucon=$?

if [ $_Ucon == 0 ]
	then
		_bttn="AWESOME"
		_diamsg=$_diamsg"   Internet access is confirmed.   "
		if [ $_canspeak == 0 ]
			then
				trans -b -p -player $_player en: :$lang "$_diamsg" > null
			else
				dialog_box
		fi
	else 
		_diamsg=$_diamsg"---   WARNING: No Internet Connection   ---"
		dialog_box
fi

This script is slowly becoming usable on any system and I dare you to stop me from going forward.

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

User avatar
ripwardog
Posts: 26
Joined: Sat Jul 27, 2024 6:28 am
Location: Middle Earth
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: YAD Startup Script

Post by ripwardog »

Final iteration?
I've relocated the variables to be established before the YAD function.
Did a little formatting cleanup for readability sake.

Code: Select all

#!/bin/bash

# Establish some string variables
# Change "Earthling" to your name or use the $USER system variable if you like.
# Example _yourname="John Doe" or _yourname=$USER.
_yourname="Earthling"
_diamsg=""
_tb="Testing Connectivity"
_day="Today is `date +%A`"
_bttn="DAMN"
_player="mpv"

# Establish some numerical variables
_hour=`date +%H`
_ret=252
_canspeak=1

# Dialog Box Function
dialog_box (){
#	Loop until the DAMN button has been pressed.
	while [ $_ret != 0 ]
	do
#	Open dialog
	    yad	--width=600 --center --fixed --on-top \
			--image="dialog-question" \
			--buttons-layout=center \
			--button="$_bttn:0" \
			--text-align=center --title="$_tb" \
			--text-align=center --text="$_diamsg" \ 

		_ret=$?
	done
}

# some systems may not have mplayer installed, EasyOS has mpv for example.
# Let's find out if mpv or mplayer or neither is installed.
command -v $_player > /dev/null;_mplins=$?
if [ $_mplins != 0 ]
	then
		_player="mplayer"
		command -v $_player > /dev/null;_mplins=$?
		if [ $_mplins != 0 ]
			then _canspeak=1
			else _canspeak=0
		fi
	else _canspeak=0
fi

#check if Google translate-shell is installed, (required for speech?)
command -v trans > null;_transin=$?

#establish the go ahead speak variable
if [ $_transin == 0 ] && [ $_canspeak == 0 ];then _canspeak=0;else _canspeak=1;fi

# May be reduced? (not recommended) (may need to be increased?)
# I've used 3 here, it seems perfect to me.
sleep 3

# for some reason I couldn't get the >= or <= operators to work in this bit of code?
if [ $_hour -ge 00 ] && [ $_hour -le 11 ]
	then
		_tod="Good morning, "
elif [ $_hour -ge 12 ] && [ $_hour -le 17 ]
	then
		_tod="Good afternoon, "
	else
		_tod="Good evening, "
fi

# Concat the strings less internet status
_diamsg=$_tod"$_yourname"". "$_day" and..."

# Test if internet is active and connected
wget -q --spider http://google.com; _Ucon=$?

if [ $_Ucon == 0 ]
	then
		_bttn="AWESOME"
		_diamsg=$_diamsg"   Internet access is confirmed.   "
		if [ $_canspeak == 0 ]
			then
				trans -b -p -player $_player en: :$lang "$_diamsg" > /dev/null
			else
				dialog_box
		fi
	else 
		_diamsg=$_diamsg"---   WARNING: No Internet Connection   ---"
		dialog_box
fi

I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve.

JusGellin
Posts: 463
Joined: Fri Jan 19, 2024 11:12 pm
Has thanked: 41 times
Been thanked: 43 times

Re: YAD Startup Script

Post by JusGellin »

@ripwardog
Wow! Thanks for sharing what you are doing. I'm going to look more at these for learning techniques for scripting.
I hope to see more of what you are doing like this.

Post Reply

Return to “Scripts”