YAD - Tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#331 Post by misko_2083 »

stemsee wrote:Here is an idea I would like to get working.

Code: Select all

mkfifo -m 755 /tmp/PIPE9 2>/dev/null
exec 9<> /tmp/PIPE9

function scan (){
PATTERN='
s/^.*ESSID://p'
MENU=`iwlist wlan2 scan | sed -rn "$PATTERN" | tac | tr '\n'  '|'`
list=`echo $MENU >/tmp/scan`
echo "menu:$MENU" >&9 &
}
export -f scan

yad --notification --icon=/usr/share/pixmaps/wireless.png --listen --text="Scanner" --command="geany /tmp/scan" <&9 &
while true
do
bash -c scan
sleep 4
done
This uses yad --notification --listen to change menu items. In this usage the menu items are the APs found from iwlist scan. The APs are one per line, "AP1"|"AP2"|"AP3" how to interpolate

Code: Select all

!bash -c connect1!/usr/share/pixmaps/wireless.png|
"AP1"|bash -c connect1|/usr/share/pixmaps/wireless.png|"AP2"!bash -c connect2!/usr/share/pixmaps/wireless.png| 
The problems I need to help to solve are 1) only scan on click.
2) click on menu item to select AP to connect with
3) show an icon next to each AP entry
This is an example that opens a text-info dialog when you click the item in the notification dialog's popup menu.

Code: Select all

#!/bin/bash
mkfifo -m 755 /tmp/PIPE9 2>/dev/null
trap "rm -f /tmp/PIPE9" EXIT

exec 9<> /tmp/PIPE9

function scan (){
PATTERN='
s/^.*ESSID://p'
iwlist wlan2 scan | sed -rn "$PATTERN" | tac >/tmp/scan
MENU="$(while read -r line ;do 
   printf "%s" "Connect $line!bash -c "yad --text-info --width=400 --height=300 <<<$(echo $line)"!/usr/share/pixmaps/wireless.png|"
done < /tmp/scan)"

echo "icon:/usr/share/pixmaps/wireless.png" >/tmp/PIPE9
echo "tooltip:Tooltip" >/tmp/PIPE9 
echo "menu:Refresh!bash -c scan!gtk-refresh|About!bash -c 'yad --about'!gtk-about||$MENU|Quit!quit!gtk-quit" >/tmp/PIPE9
}
export -f scan

yad --notification --listen --text="Scanner" --command="geany /tmp/scan" <&9 & NOTIF_ICON=$!
sleep 1
bash -c scan

wait $NOTIF_ICON

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#332 Post by step »


[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

stemsee

#333 Post by stemsee »

misko_2083 wrote: This is an example that opens a text-info dialog when you click the item in the notification dialog's popup menu.
This is fantastic magic!
I have adapted your code a bit. removed the bashism '<<<' so more portable > '/bin/sh'
Seems to be working well.
I call the script netmenu

netmenu-0.1-alpha

Code: Select all

#!/bin/sh
#

depsfn () {        # check dependencies
	dhclu=`busybox which udhcpc`
	dhcld=`busybox which dhcpcd`
	if [[ ! -z "${dhclu}" ]]; then
		dhl1="udhcpc -A 8 -p /var/run/udhcpc.pid -s /usr/share/udhcpc/default.script -i"
		dhl2="/usr/share/udhcpc/default.script deconfig"
		dhl3="udhcpc"
	elif [[ -z "${dhclu}" ]]; then
		dhl1="dhcpcd -z"
		dhl2="dhcpcd -k"
		dhl3="dhcpcd"
	elif [[ -z "${dhclu}" && -z "${dhcld}" ]]; then
		zenity --info --text "$dhclu and $dhcld not found"
		echo "$dhclu and $dhcld not found"
	fi
	export dhl1
	export dhl2
	export dhl3
}
depsfn
interfacefn () {
for wit in `ls /sys/class/net`
do
	case $wit in
		wl*) int=${wit};;
		eth0|eth2|eth4|eth6|enp2s0f2) eth=${wit};;
		eth1|eth3|eth5|eth7) eths=${wit};;
		enp0s20u3|usb0|usb1) usb0=${wit};;
	esac
done
}
interfacefn
if [[ "$1" = eth0-u ]]; then
	ifconfig ${eth} up
	${dhl1} ${eth}
	exit
fi
if [[ "$1" = eth0-d ]]; then
	ifconfig ${eth} down
	${dhl2} ${eth}
	exit
fi
if [[ "$1" = usb0-u ]]; then
	ifconfig ${usb0} up
	${dhl1} ${usb0}
	exit
fi
if [[ "$1" = usb0-d ]]; then
	ifconfig ${usb0} down
	${dhl2} ${usb0}
	exit
fi
mkfifo -m 755 /tmp/PIPE9 2>/dev/null
exec 9<> /tmp/PIPE9
if [[ -z "$int" ]]; then
	int=`ls /sys/class/net/ | grep -v lo | grep -v eth | grep -v usb | head -1`
fi
export int
ifconfig $int up
touch /tmp/ap

function cleanfinish (){
	rm -f /tmp/PIPE9
	rm -f /tmp/netsel
	rm -f /tmp/scan
	rm -f /tmp/scan2
	rm -f /tmp/n
	ifconfig $int down
	"$dhl2" $int
}
trap cleanfinish EXIT

function connectfn (){
		details=$(yad --form --columns=2 --field="Password:H" -entry "" --field="ID" -entry "")
		password=`echo $details | cut -f1 -d'|'`
		id=`echo $details | cut -f2 -d'|'`
		
	while read line
	do
		ssid="$line"
	done < /tmp/ap

if [[ ! -z "$id" && ! -z "$password" ]]; then
	echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1
update_config=1

network={
	ssid='$ssid'
	identity='$id'
	password='$password'
	key_mgmt=WPA-EAP
	eap=PEAP
	#ca_cert='$HOME/.wifi-connect/ca.pem'
	#phase1='peaplabel=0'
	phase2='auth=MSCHAPV2'
}
" | tr "'" '"' > /tmp/netsel
elif [[ ! -z "$password" && -z "$id" ]]; then
	wpa_passphrase "$ssid" "$password" > /tmp/netsel
elif [[ -z "$password" && -z "$id" ]]; then
		echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1
update_config=1

network={
	ssid='$ssid'
	#bssid=$address
	key_mgmt=NONE
}
" | tr "'" '"'  > /tmp/netsel
fi
CURRENTIPS=$(ip addr show | grep $int | grep inet | awk '{print $2}')
ifconfig $int down
for I in ${CURRENTIPS}; do ip addr del $I dev ${int}; done 2> /dev/null
ip route flush dev $int 2> /dev/null
killall -9 $dhl3 2> /dev/null
killall -9 wpa_supplicant 2> /dev/null
rm -f /var/db/$dhl3-${int}.lease 2> /dev/null
rm -f /var/lib/$dhl3/$dhl3-${int}.lease 2> /dev/null
rm -f /var/run/wpa_supplicant/$int 2> /dev/null
ifconfig $int up 2> /dev/null
wpa_supplicant -Dwext -i$int -c/tmp/netsel -B
$dhl1 $int
}
export -f connectfn

function inotify (){
while inotifywait -e close_write /tmp/ap
do 
	bash -c connectfn
	sleep 10
done
}
export -f inotify
bash -c inotify &

function scan (){ 

PATTERN='s/^.*key:([onf]*)/~\1~/p
s/^.*ESSID://p'
iwlist $int scan | sed -rn "$PATTERN" >/tmp/scan2
cat /tmp/scan2 | grep -v [onf] > /tmp/scan
cat /tmp/scan2 | grep -e'~on~' -e '~off~' > /tmp/encryption

MENU="$(while read -r line ;do
   printf "%s" "$int ${line}!bash -c "echo "$line" > /tmp/ap"!/usr/share/pixmaps/wireless.png|" 
done < /tmp/scan)" 
sixth="Ethernet connect!netmenu eth0-u!/usr/share/icons/oxygen/22x22/devices/network-wired.png|"
seventh="Ethernet disconnect!netmenu eth0-d!/usr/share/icons/oxygen/22x22/devices/network-wiredx.png|"
tenth="Connect mobile!netmenu usb0-u!/usr/share/icons/oxygen/22x22/devices/phone.png|"
eleventh="Disconnect mobile!netmenu usb0-d!/usr/share/icons/oxygen/22x22/devices/phonex.png|"
echo "icon:/usr/share/pixmaps/wireless.png" >/tmp/PIPE9 
echo "tooltip:$int" >/tmp/PIPE9 
echo "menu:Refresh!bash -c scan!gtk-refresh|About!bash -c 'yad --about'!gtk-about||$MENU|$sixth$seventh$tenth$eleventh||Quit!quit!gtk-quit" >/tmp/PIPE9 
} 
export -f scan 

yad --notification --listen --text="Scanner" --command="geany /tmp/encryption" <&9 & NOTIF_ICON=$! 
sleep 1 
bash -c scan 

wait $NOTIF_ICON
Attachments
netmenu.png
(5.58 KiB) Downloaded 1241 times
netmenu-0.1-alpha.png
(16.28 KiB) Downloaded 1264 times
netmenu-0.1-alpha.png
(21.08 KiB) Downloaded 1245 times
netmenu.png
(25.52 KiB) Downloaded 1236 times
Last edited by stemsee on Tue 30 Jan 2018, 10:19, edited 1 time in total.

stemsee

#334 Post by stemsee »

The MENU portion of this code preserves spaces for the yad menu, but when printed to the /tmp/ap file the space within quoted AP name disappears! i have tried variations of echo, printf, tee, redirections, bracketing, quoting ... but nothing I have tried works. There is one way that prints the desired format "My Way Cafe" but it does not print the selected item, just the first one slips through. Here is the suspect code that loses the space

Code: Select all

MENU="$(while read -r line ;do
   printf %s "${line}!bash -c \"echo "$line" > /tmp/ap\"!/usr/share/pixmaps/wireless.png|" 
done < /tmp/text)"
this is the variation that prints a line, not the selected line, in correct format

Code: Select all

MENU="$(while read -r line ;do
   printf %s "${line}!bash -c \"$(echo "$line" > /tmp/ap)\"!/usr/share/pixmaps/wireless.png|" 
done < /tmp/text)"
/tmp/ap is being watched by inotifywait -e close_write which triggers 'bash -c connectfn'. It connects after first scan without selecting AP, because one slips through! I know this is more of a coding issue than a yad issue, although it is in a yad menu, but any further help appreciated.
Attachments
xscreenshot-20180130T103717.png
menu
(36.85 KiB) Downloaded 1160 times
xscreenshot-20180130T094053.png
missing spaces
(1.33 KiB) Downloaded 1163 times
xscreenshot-20180130T094038.png
correct spacing
(1.78 KiB) Downloaded 1160 times

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#335 Post by misko_2083 »

stemsee wrote:The MENU portion of this code preserves spaces for the yad menu, but when printed to the /tmp/ap file the space within quoted AP name disappears! i have tried variations of echo, printf, tee, redirections, bracketing, quoting ... but nothing I have tried works. There is one way that prints the desired format "My Way Cafe" but it does not print the selected item, just the first one slips through. Here is the suspect code that loses the space

Code: Select all

MENU="$(while read -r line ;do
   printf %s "${line}!bash -c "echo "$line" > /tmp/ap"!/usr/share/pixmaps/wireless.png|" 
done < /tmp/text)"
this is the variation that prints a line, not the selected line, in correct format

Code: Select all

MENU="$(while read -r line ;do
   printf %s "${line}!bash -c "$(echo "$line" > /tmp/ap)"!/usr/share/pixmaps/wireless.png|" 
done < /tmp/text)"
/tmp/ap is being watched by inotifywait -e close_write which triggers 'bash -c connectfn'. It connects after first scan without selecting AP, because one slips through! I know this is more of a coding issue than a yad issue, although it is in a yad menu, but any further help appreciated.
The problems are with escaping quotes and spaces.

As a workaround you can use print %q
printf %q "$line" escapes quotes and spaces
"MC\ cm"\ ~on~
later when you use echo -e

Code: Select all

$ echo -e  "MC\ cm"\ ~on~
it will return
"MC cm" ~on~

Code: Select all

MENU="$(while read -r line ;do
   WIFI="$(printf %q "$line")"
   printf "%s" "${line}!bash -c 'echo -e ${WIFI} > /tmp/ap'!/usr/share/pixmaps/wireless.png|"
done < /tmp/text)"
Works for echo too.

Code: Select all

MENU="$(while read -r line ;do
   WIFI="$(printf %q "$line")"
   echo -ne "${line}!bash -c 'echo -e ${WIFI} > /tmp/ap'!/usr/share/pixmaps/wireless.png|"
done < /tmp/text)"
Note that special characters in the hotspot name like "!" and "|" will make a mess in the righ-click popup because they are used as the splitters in yad --notification dialog.

One more thing I spoted in the script. No need for opening pipe and extra cat command.

Code: Select all

cat file | grep "something" > file2
grep has built in that can read files

Code: Select all

grep "something"  file > file2

stemsee

#336 Post by stemsee »

Thanks @ misko_2083

'cat grep' novice error. I now know 'grep -v -e' for multiple inversions, after some research. grep --help yields only '-v, --invert-match select non-matching lines'

Your code now works perfectly. A great lesson in itself!

I had already implemented a workaround, in the pop up gui for password/id input, I simply listed scan results selector.

Thanks again
stem
Attachments
netmenu.fake.gz
(10.11 KiB) Downloaded 144 times

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#337 Post by fredx181 »

Hello,
I'm having a similar problem as stemsee had above (space not showing), but for me it's the underscore not showing in the yad notification menu.
For example, I have filenames in /root/test contaning underscores

Code: Select all

root@live:~/test# ls
my_file_1  my_file_2  my_file_3
My goal is to list these files in the yad menu and process them further, however the processing works fine (open the files with leafpad, for example), the filenames are missing underscores in the menu (see screenshot)
Anyone understands what's happening here? When I uncomment "set -x" in the create_menu function, I see all names displayed correctly, here's simplified script:

Code: Select all

#!/bin/bash

on_click() {
# Help text
echo -en " Helptext \n\n text" | yad --title="Help" --text-info --wrap --fore=white --back="gray26" --fontname=Bold --height=350 --width=500 --button="gtk-close"
}
export -f on_click

quit_tray () {
pidnoti="`ps -eo pid,cmd | grep -v grep | grep "yad --notification --text=$POPUP_TEXT" | awk '{ print $1 }'`"
kill $pidnoti 2> /dev/null
rm -f $PIPE
}
export -f quit_tray

exec_file () {
leafpad "$DIR/$file"
# and/or more stuff to do with $file
}
export -f exec_file 

create_menu () {
# set -x
export DIR="/root/test"
LISTFILES="$(ls "$DIR" 2> /dev/null)"
MENU=$(
while read -r file; do
[ -n "$LISTFILES" ] && echo -n "${file}! bash -c 'export file=$file; exec_file'!gtk-ok|"
done <<< "$LISTFILES")
}
export -f create_menu

TRAY_ICON="gtk-directory"
POPUP_TEXT="Menu"
export PIPE=$(mktemp -u --tmpdir ${0##*/}.XXXXXXXX)
mkfifo $PIPE

# attach a file descriptor to the file
exec 3<> $PIPE

create_menu
GET_MENU="${MENU}|Exit! bash -c quit_tray!gtk-quit"
echo menu:"${GET_MENU}" 2> /dev/null >&3
 
GUI=$(yad --notification --text="$POPUP_TEXT" --listen \
--image="$TRAY_ICON" \
--text="$POPUP_TEXT" \
--command="bash -c on_click" <&3 ) & NOTIF_ICON=$!
wait $NOTIF_ICON  #misko_2083
Attachments
2018-02-04-124146_257x244_scrot.png
yad notification menu not showing underscores
(77.96 KiB) Downloaded 1033 times

some1
Posts: 117
Joined: Thu 17 Jan 2013, 11:07

#338 Post by some1 »

fredx181
Yad/the gui probably interpretes a single underscore - as an indication of an accellerator-key/shortcut.

Replace:

Code: Select all

[ -n "$LISTFILES" ] && echo -n "${file}....
with

Code: Select all

[ -n "$LISTFILES" ] && echo -n "${file//_/__}....
and you will see the filenames with underscore(s) in the tray-menu.

Please observe that file is also a command-name.
You may want to use a different varname.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#339 Post by fredx181 »

some1 wrote:Replace:
Code:
[ -n "$LISTFILES" ] && echo -n "${file}....

with
Code:
[ -n "$LISTFILES" ] && echo -n "${file//_/__}....

and you will see the filenames with underscore(s) in the tray-menu.
Great !! Works perfectly, didn't expect the solution would be that simple, many thanks, some1 !
Please observe that file is also a command-name.
Yeah, good advice, better be safe.

EDIT: Here's the program I made with the concept of a file list in the menu and processing them further (in this case by loading or unloading a .squashfs or .sfs module)
http://murga-linux.com/puppy/viewtopic. ... 262#982262
I'm pretty excited about what y'all can do with the yad tray icon menu :D
Thanks stemsee and misko_2083 !!

Fred

matchpoint
Posts: 168
Joined: Fri 26 Jan 2018, 20:54

#340 Post by matchpoint »

Hey Puppers,

Since I'm not yet too familiar with YAD, I'm hoping someone here can show me what this might look like piped into YAD. TIA.

Code: Select all

#!/bin/bash

echo "*** Temperature scale conversion ***"
echo "1. Convert Celsius to Fahrenheit"
echo "2. Convert Fahrenheit to Celsius"
echo -n "Select 1 or 2, then Enter:"
read choice

if [ $choice -eq 1 ]
then
  echo -n "Enter temperature (C) : "
  read tc
  # formula Tf=(9/5)*Tc+32
  tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
  echo "$tc C = $tf F"
elif [ $choice -eq 2 ]
then
  echo -n "Enter temperature (F) : "
  read tf
  # formula Tc=(5/9)*(Tf-32)
  tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
  echo "$tf F = $tc C"
else
  echo "Please select 1 or 2 only"
  exit 1
fi

echo -n "
-->  Press any key to exit "
read echoice

exit 0

april

#341 Post by april »

What command would you like to see . Same result yad first or last
Attachments
capture27172.png
(52.86 KiB) Downloaded 98 times

matchpoint
Posts: 168
Joined: Fri 26 Jan 2018, 20:54

#342 Post by matchpoint »

Hi April, and thanks. What I'm asking for is an example for having YAD handle the exchange. Then, maybe it's not YAD I'm after but GtkDialog. There's a nice example here, several here, and an intro by zigbert here. As not to spend much time trying to reinvent the wheel, and considering the amount of user input remains the same, I assigned an icon to my script and placed it on the launch bar for easy-peasy access.
Attachments
cel2fah-launch.jpg
(8.19 KiB) Downloaded 839 times

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#343 Post by fredx181 »

Hi matchpoint, something like this maybe ?

Code: Select all

#!/bin/bash

SETUP=`yad --title="Temperature scale conversion" --center --text="   <b>*** Temperature scale conversion ***</b>" \
--width=400 \
--window-icon="preferences-system" --form  \
--field=" Select: :CB" "Convert Celsius to Fahrenheit!Convert Fahrenheit to Celsius" \
--field=" Set temperature: :NUM" "1!1..500!1" \
--button="gtk-cancel:1" --button="gtk-ok:0"`
[[ $? -ne 0 ]] && exit 1

export CHOICE=$(echo $SETUP | cut -d "|" -f 1)
export TEMP_=$(echo $SETUP | cut -d "|" -f 2 | cut -f1 -d".")

case $CHOICE in
"Convert Celsius to Fahrenheit")
  tc=$TEMP_
  # formula Tf=(9/5)*Tc+32
  tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
  echo "$tc C = $tf F" | yad --title="Celsius to Fahrenheit" --center --text-info --width=300 --button="gtk-close:0"
;;
"Convert Fahrenheit to Celsius")
  tf=$TEMP_
  # formula Tc=(5/9)*(Tf-32)
  tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
  echo "$tf F = $tc C" | yad --title="Fahrenheit to Celsius" --center --text-info --width=300 --button="gtk-close:0"
;;
esac
EDIT: Removed "--text-align=center" from above script for compatibility with older yad version

Fred
Last edited by fredx181 on Sat 10 Feb 2018, 16:54, edited 2 times in total.

matchpoint
Posts: 168
Joined: Fri 26 Jan 2018, 20:54

#344 Post by matchpoint »

Maybe Fred, it didn't open anything though.

It looks like your intention was for me to save it as a script and have it open a kind of message box, correct?

What's the "esac" on the end? YAD lingo?

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#345 Post by fredx181 »

matchpoint wrote:It looks like your intention was for me to save it as a script and have it open a kind of message box, correct?

What's the "esac" on the end? YAD lingo?
Yes, save it for example as "tempconv" and make excutable:

Code: Select all

chmod +x tempconv
And run it.

The esac in not yad code, it's builtin bash code, "case ... in", should be finished with esac, just like if ....; fi
Only where it shows "yad ......." is yad code.

edit: hopefully you have higher yad version than 0.12 (which is included in some Puppies), probably works only with version higher than 0.12 (or maybe needs some adjustments to make it work with 0.12)
edit2: edited the script above slightly so there's better chance it works with older yad.

Fred
Attachments
2018-02-10-171517_402x176_scrot.png
Convert temperature
(26.13 KiB) Downloaded 831 times

matchpoint
Posts: 168
Joined: Fri 26 Jan 2018, 20:54

#346 Post by matchpoint »

Ha! I forgot to save the paste before running.

That the console now complains with "--text-align=center" tells me we have a Zenity/YAD version difference, right?

I've come across these type messages before.

Ultimately though, I'd like the choice selection to be made like in the original, by keystroke, not mouse click.

Maybe one more example, Fred. Then I think I may be able to run with it.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#347 Post by fredx181 »

matchpoint wrote:Ha! I forgot to save the paste before running.

That the console now complains with "--text-align=center" tells me we have a Zenity/YAD version difference, right?

I've come across these type messages before.

Ultimately though, I'd like the choice selection to be made like in the original, by keystroke, not mouse click.

Maybe one more example, Fred. Then I think I may be able to run with it.
Probably Zenity doesn't have "--text-align=center" :?: , btw, I removed that from script above to be compatible with older yad versions. (see also edit in my previous post)

Let me understand, you want to run the script in terminal, type 1 or 2 and the rest by YAD gui, or do you want to type the temperature also in terminal and use YAD only for the final output ?

Fred
Last edited by fredx181 on Sat 10 Feb 2018, 17:31, edited 1 time in total.

matchpoint
Posts: 168
Joined: Fri 26 Jan 2018, 20:54

#348 Post by matchpoint »

Fred, thanks and relax. I'll get back to this little later.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#349 Post by fredx181 »

Hi matchpoint, I think I sort of get it now what you'd like, anyway here's another with 2 boxes where you can type in the choices, ok, I'll relax now :wink:

Code: Select all

#!/bin/bash

SETUP=`yad --title="Temperature scale conversion" --center --text="<b>*** Temperature scale conversion ***</b> \n Type 1 to convert Celsius to Fahrenheit \n Type 2 to convert Fahrenheit to Celsius" \
--width=400 \
--window-icon="preferences-system" --form  \
--field=" Type 1 or 2: : " "" \
--field=" Type temperature: : " "" \
--button="gtk-cancel:1" --button="gtk-ok:0"`
[[ $? -ne 0 ]] && exit 1

export CHOICE=$(echo $SETUP | cut -d "|" -f 1)
export TEMP_=$(echo $SETUP | cut -d "|" -f 2)

case $CHOICE in
1)
  tc=$TEMP_
  # formula Tf=(9/5)*Tc+32
  tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
  echo "$tc C = $tf F" | yad --title="Celsius to Fahrenheit" --center --text-info --width=300 --button="gtk-close:0"
;;
2)
  tf=$TEMP_
  # formula Tc=(5/9)*(Tf-32)
  tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
  echo "$tf F = $tc C" | yad --title="Fahrenheit to Celsius" --center --text-info --width=300 --button="gtk-close:0"
;;
esac
Fred

matchpoint
Posts: 168
Joined: Fri 26 Jan 2018, 20:54

#350 Post by matchpoint »

Fred, I'd like the dialog to run and read like my script, except with one line spacings between each entry and not yet requesting temperature input until a choice has been made. Once made however, the dialog should then switch to an "Enter temperature (C/F)" sequence, rest for 3 or so seconds after user input, then exit, effectively removing the need to "Press any key to exit."

:D Can do?

You know, even this script I have should sign off like how I've described.

And how do I get spacing between lines when dumping to stdout?

Like between these opening echos?

Code: Select all

#!/bin/bash

echo "*** Temperature scale conversion ***"
echo "Convert Celsius to Fahrenheit = 1"
echo "Convert Fahrenheit to Celsius = 2"
echo -n "Select 1 or 2, then Enter:"
read choice

if [ $choice -eq 1 ]
then
  echo -n "Enter temperature (C) : "
  read tc
  # formula Tf=(9/5)*Tc+32
  tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
  echo "$tc C = $tf F"
elif [ $choice -eq 2 ]
then
  echo -n "Enter temperature (F) : "
  read tf
  # formula Tc=(5/9)*(Tf-32)
  tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
  echo "$tf F = $tc C"
else
  echo "Please select 1 or 2 only"
  exit 1
fi

echo -n "
-->  Press any key to exit "
read echoice

exit 0

Post Reply