1

So, I have this script:

#!/bin/sh
#InputBox1Test

title=$(zenity --entry --text 'Type what you want your Notification Title to Say!' --title 'Notification')
text=$(zenity --entry --text 'Type what you want your Notification body to Say!' --title 'Notification')
DISPLAY=:0.0 notify-send "$title" "$text"

This script opens a zenity window, and walks you through generating a notification. I want to make it exit when you press cancel. How do I do that?

4
  • Below each line add a new line echo "$?" and then test what zenity returns for each button plus escape key. Then later put in test of values you wish to trap and act upon. Feb 19, 2020 at 22:55
  • it comes out for cancel to "1" and okay "0" how do I make the if statement Feb 19, 2020 at 23:39
  • You should really test Escape key and Ctrl + F4 key too but I'll write an answer for 1 and 0. Feb 20, 2020 at 1:18
  • @WinEunuuchs2Unix Thanks alot! this made my day! Feb 20, 2020 at 13:20

1 Answer 1

1

Assuming that return value is 1 for "cancel" and 0 for "ok" you will want to use:

#!/bin/sh
#InputBox1Test

title=$(zenity --entry --text 'Type what you want your Notification Title to Say!' --title 'Notification')
[[ "$?" != "0" ]] && exit 1
text=$(zenity --entry --text 'Type what you want your Notification body to Say!' --title 'Notification')
[[ "$?" != "0" ]] && exit 1
DISPLAY=:0.0 notify-send "$title" "$text"

You can make a longer traditional check like this:

if [[ "$?" != "0" ]] ; then
    exit 1
fi

however I like the shortcut of:

[[ "$?" != "0" ]] && exit 1

No matter which method you choose the importance is consistency with your programming style so those that follow in your footsteps and maintain your code can think inside your head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.