Little Girl's Mostly Linux Blog

KDialog


KDialog

This page was last updated on October 10, 2011.

This page has examples of Kdialog commands and their results being used in scripts. There are subtle differences, so please read the commented descriptions in each script.
  • Some examples use HTML <br> tags. You could use \n in a few of them instead of the <br> tags, but since not all of them accept it, it’s not demonstrated below.
  • Many examples use the old and new command substitution to combine commands. It isn’t always necessary to do so, and when it’s possible to achieve the same result without command substitution, other examples are shown.
  • Most examples use the Bash echo command as the action taken.
You can make the scripts much more complicated by substituting and/or adding other text and/or commands, and/or by by combining the scripts into a bigger script.
 
As always, if you find errors in any of my stuff or if you have any suggestions or ideas, please let me know in the comment box at the bottom of the page or by email with the link in the upper right corner of the page.
 
All the scripts are now available in this zip file.

Table of Contents


Checklist

These examples launch a kdialog window that allows you to choose one or more items from a selection in a list. A different action (echoing a message to the terminal window) is taken based on whether you make a choice, and if so, which choice you make, whether you make no choice at all, or whether something goes wrong.

Each item in the list needs a key that is used by the script to identify which list items were chosen, a label that is used to communicate the choices to the user, and a setting to let the Kdialog command know whether to set the list item as selected when the list loads. In the examples below, numbers are used as the key to identify each list item, but you can use lower or upper case alphabet letters, words, or a combination of letters and numbers. Whatever you use will be what the script looks for to decide what to do.

Example 1:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a combo box from which to make a choice. When you make the choice,     #
# command substitution is used to save your choice to a variable that is     #
# used to determine which of the six possible messages to display in the     #
# terminal window based on which choice you make or whether you make one at  #
# all.                                                                       #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos the number or numbers (each surrounded in "quotes" with a #
# space between them) you chose to the terminal window.                      #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=`kdialog --checklist "CHOOSE:" 1 "RED" on 2 "BLUE" off 3 "GREEN" off;`;

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a combo box from which to make a choice. When you make the choice,     #
# command substitution is used to save your choice to a variable that is     #
# used to determine which of the six possible messages to display in the     #
# terminal window based on which choice you make or whether you make one at  #
# all.                                                                       #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos the number or numbers (each surrounded in "quotes" with a #
# space between them) you chose to the terminal window.                      #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --checklist "CHOOSE:" 1 "RED" on 2 "BLUE" off 3 "GREEN" off;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a two-line message (created by     #
# using an HTML <br> tag) that offers you a combo box from which to          #
# make a choice. When you make the choice, command substitution is used to   #
# save your choice to a variable that is used to determine which of the six  #
# possible messages to display in the terminal window based on which choice  #
# you make or whether you make one all.                                      #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos the number or numbers (each surrounded in "quotes" with a #
# space between them) you chose to the terminal window.                      #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --checklist "LINE1<br>LINE2" 1 "RED" on 2 "BLUE" off 3 "GREEN" off;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 4:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a combo box from which to make a choice. When you make the choice,     #
# command substitution is used to save your choice to a variable that is     #
# used to determine which of the six possible messages to display in the     #
# terminal window based on which choice you make or whether you make one at  #
# all.                                                                       #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos the number or numbers you chose to the terminal window,   #
# each on a separate line.                                                   #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --separate-output --checklist "CHOOSE:" 1 "RED" on 2  "BLUE" off 3 "GREEN" off;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 5:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a two-line message (created by     #
# using an HTML <br> tag) that offers you a combo box from which to          #
# make a choice. When you make the choice, command substitution is used to   #
# save your choice to a variable that is used to determine which of the six  #
# possible messages to display in the terminal window based on which choice  #
# you make or whether you make one all.                                      #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos the number or numbers you chose to the terminal window,   #
# each surrounded in "quotes" with a space between them.                     #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --separate-output --checklist "LINE1<br>LINE2" 1 "RED" on 2  "BLUE" off 3 "GREEN" off;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Combo box

These examples launch a kdialog window that allows you to choose one item from a selection in a combo box. A different action (echoing a message to the terminal window) is taken based on whether you make a choice, and if so, which choice you make, whether you make no choice at all, or whether something goes wrong.
Example 1:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a combo box from which to make a choice. When you make the choice,     #
# command substitution is used to save your choice to a variable that is     #
# used to determine which of the six possible messages to display in the     #
# terminal window based on which choice you make or whether you make one at  #
# all.                                                                       #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos a color or a reminder to you to choose a color to the     #
# terminal window.                                                           #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=`kdialog --combobox "CHOOSE:" "RED" "BLUE" "GREEN";`;

if [ "$?" = 0 ]; then
	if [ "$variable" = RED ]; then
		echo "YOU CHOSE RED";
	elif [ "$variable" = BLUE ]; then
		echo "YOU CHOSE BLUE";
	elif [ "$variable" = GREEN ]; then
		echo "YOU CHOSE GREEN";
	else
		echo "PLEASE CHOOSE A COLOR";
	fi;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a combo box from which to make a choice. When you make the choice,     #
# command substitution is used to save your choice to a variable that is     #
# used to determine which of the six possible messages to display in the     #
# terminal window based on which choice you make or whether you make one at  #
# all.                                                                       #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos a color or a reminder to you to choose a color to the     #
# terminal window.                                                           #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --combobox "CHOOSE:" "RED" "BLUE" "GREEN";);

if [ "$?" = 0 ]; then
	if [ "$variable" = RED ]; then
		echo "YOU CHOSE RED";
	elif [ "$variable" = BLUE ]; then
		echo "YOU CHOSE BLUE";
	elif [ "$variable" = GREEN ]; then
		echo "YOU CHOSE GREEN";
	else
		echo "PLEASE CHOOSE A COLOR";
	fi;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a two-line message (created by     #
# using an HTML <br> tag) that offers you a combo box from which to          #
# make a choice. When you make the choice, command substitution is used to   #
# save your choice to a variable that is used to determine which of the six  #
# possible messages to display in the terminal window based on which choice  #
# you make or whether you make one at all.                                   #
#                                                                            #
# The combo box returns 0 if the Enter key or the OK button is pressed. In   #
# that case, the script executes the action you assigned to 0 - in this      #
# example it echos a color or a reminder to you to choose a color to the     #
# terminal window.                                                           #
#                                                                            #
# The combo box returns 1 if the Esc key or Cancel button is pressed. In     #
# that case, the combo box bypasses the choices in the combo box and chooses #
# to take the action in the else section - in this example it echos YOU      #
# CHOSE CANCEL to the terminal window.                                       #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --combobox "LINE1<br>LINE2" "RED" "BLUE" "GREEN");

if [ "$?" = 0 ]; then
	if [ "$variable" = RED ]; then
		echo "YOU CHOSE RED";
	elif [ "$variable" = BLUE ]; then
		echo "YOU CHOSE BLUE";
	elif [ "$variable" = GREEN ]; then
		echo "YOU CHOSE GREEN";
	else
		echo "PLEASE CHOOSE A COLOR";
	fi;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Get existing directory

These examples launch a kdialog window that allows you to choose or create a directory. A different action (echoing a message to the terminal window) is taken based on whether you make a choice, and if so, which choice you make, whether you make no choice at all, or whether something goes wrong.
Example 1:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window that lets you browse to a directory or  #
# create a new one. Command substitution is used to echo a specific message  #
# to the terminal window based on which directory you choose or create, if   #
# any or if somthing goes wrong.                                             #
#                                                                            #
# The command returns 0 if the New Folder... button is used to create a new  #
# folder and then the OK button or the Enter key is pressed, or if an        #
# existing folder is selected and the OK button or the Enter key is pressed. #
# It then executes the command specified for a return of 0 - in this example #
# it echos the path to the folder you created or selected to the terminal    #
# window.                                                                    #
#                                                                            #
# The command returns 1 if the Esc key or Cancel button is pressed. It then  #
# echos YOU CHOSE CANCEL to the terminal window,                             #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=`kdialog --getexistingdirectory *;`;

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window that lets you browse to a directory or  #
# create a new one. Command substitution is used to echo a specific message  #
# to the terminal window based on which directory you choose or create, if   #
# any or if somthing goes wrong.                                             #
#                                                                            #
# The command returns 0 if the New Folder... button is used to create a new  #
# folder and then the OK button or the Enter key is pressed, or if an        #
# existing folder is selected and the OK button or the Enter key is pressed. #
# It then executes the command specified for a return of 0 - in this exampl  #
# it echos the path to the folder you created or selected to the terminal    #
# window.                                                                    #
#                                                                            #
# The command returns 1 if the Esc key or Cancel button is pressed. It then  #
# echos YOU CHOSE CANCEL to the terminal window,                             #
#                                                                            #
# If anything else happens, the script executes the action you assigned to   #
# the else section - in this example it echos ERROR to the terminal window.  #
##############################################################################

variable=$(kdialog --getexistingdirectory *;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Get open file name

These examples launch a kdialog window that expects you to choose a file to open. A different action is taken based on whether you make a choice, and if so, which choice you make, whether you make no choice at all, or whether something goes wrong.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file to open starting in the current directory. Command substitution is   #
# used to save the path to the file you choose to a variable. The Kdialog     #
# command is tested to determine which action to take.                        #
#                                                                             #
# The Kdialog command returns 0 if the Open button is pressed or if the Enter #
# key is pressed when a file name has been selected or if a file name is      #
# double-clicked. In that case, the script executes the action you assigned   #
# to 0 - in this example it echos the variable that contains your selection   #
# to the terminal window.                                                     #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=`kdialog --getopenfilename .`;

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file to open starting in the current directory. Command substitution is   #
# used to save the path to the file you choose to a variable. The Kdialog     #
# command is tested to determine which action to take.                        #
#                                                                             #
# The Kdialog command returns 0 if the Open button is pressed or if the Enter #
# key is pressed when a file name has been selected or if a file name is      #
# double-clicked. In that case, the script executes the action you assigned   #
# to 0 - in this example it echos the variable that contains your selection   #
# to the terminal window.                                                     #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getopenfilename .);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file to open starting in the specified directory (in this example it's    #
# the /PATH/ directory). Command substitution is used to save the path to the #
# file you choose to a variable. The Kdialog command is tested to determine   #
# which action to take.                                                       #
#                                                                             #
# The Kdialog command returns 0 if the Open button is pressed or if the Enter #
# key is pressed when a file name has been selected or if a file name is      #
# double-clicked. In that case, the script executes the action you assigned   #
# to 0 - in this example it echos the variable that contains your selection   #
# to the terminal window.                                                     #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getopenfilename /PATH/.);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 4:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file with the specified extension (in this example the .txt and .html     #
# extensions are used) to open starting in the current directory. Command     #
# substitution is used to save the path to the file you choose to a variable. #
# The Kdialog command is tested to determine which action to take.            #
#                                                                             #
# The Kdialog command returns 0 if the Open button is pressed or if the Enter #
# key is pressed when a file name has been selected or if a file name is      #
# double-clicked. In that case, the script executes the action you assigned   #
# to 0 - in this example it echos the variable that contains your selection   #
# to the terminal window.                                                     #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getopenfilename . "*.txt *.html";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 5:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file of the specified mime type (in this example the image/png, the       #
# txt/html, and the text/plain mime types are used) to open starting in the   #
# current directory. Command substitution is used to save the path to the     #
# file you choose to a variable. The Kdialog command is tested to determine   #
# which action to take.                                                       #
#                                                                             #
# The Kdialog command returns 0 if the Open button is pressed or if the Enter #
# key is pressed when a file name has been selected or if a file name is      #
# double-clicked. In that case, the script executes the action you assigned   #
# to 0 - in this example it echos the variable that contains your selection   #
# to the terminal window.                                                     #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getopenfilename . "image/png text/html text/plain";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Get save file name

These examples launch a kdialog window that expects you to choose a file. A different action is taken based on whether you make a choice, and if so, which choice you make, whether you make no choice at all, or whether something goes wrong.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file starting in the current directory. Command substitution is used to   #
# save the path to the file you choose to a variable. The Kdialog command is  #
# tested to determine which action to take.                                   #
#                                                                             #
# The Kdialog command returns 0 if a file is selected and the Save button is  #
# pressed, or if a file name is double-clicked. In that case, the script      #
# executes the action you assigned to 0 - in this example it echos the        #
# variable that contains your selection to the terminal window.               #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=`kdialog --getsavefilename .;`;

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file starting in the current directory. Command substitution is used to   #
# save the path to the file you choose to a variable. The Kdialog command is  #
# tested to determine which action to take.                                   #
#                                                                             #
# The Kdialog command returns 0 if a file is selected and the Save button is  #
# pressed, or if a file name is double-clicked. In that case, the script      #
# executes the action you assigned to 0 - in this example it echos the        #
# variable that contains your selection to the terminal window.               #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getsavefilename .;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file starting in the directory specified by the PATH in the command.      #
# Command substitution is used to save the path to the file you choose to a   #
# variable. The Kdialog command is tested to determine which action to take.  #
#                                                                             #
# The Kdialog command returns 0 if a file is selected and the Save button is  #
# pressed, or if a file name is double-clicked. In that case, the script      #
# executes the action you assigned to 0 - in this example it echos the        #
# variable that contains your selection to the terminal window.               #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getsavefilename /PATH/.;);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 4:
#!/bin/bash
###############################################################################
# This script opens a Kdialog file browsing window that expects you to choose #
# a file of the specified type (in this example files with the .txt extension #
# are acceptable) starting in the current directory. Command substitution is  #
# used to save the path to the file you choose to a variable. The Kdialog     #
# command is tested to determine which action to take.                        #
#                                                                             #
# The Kdialog command returns 0 if a file is selected and the Save button is  #
# pressed, or if a file name is double-clicked. In that case, the script      #
# executes the action you assigned to 0 - in this example it echos the        #
# variable that contains your selection to the terminal window.               #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --getsavefilename . "*.txt";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Input box

These examples launch a kdialog window that asks you to type in some input. A different action is taken based on whether you make a choice, and if so, which choice you make, whether you make no choice at all, or whether something goes wrong.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with a single-line message that asks you #
# to type in some input. Command substitution is used to save your input to a #
# variable. The script then tests the Kdialog command's return to determine   #
# which action to take based on the input you entered or whether you entered  #
# any at all.                                                                 #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or the OK button is pressed. #
# In that case, the script executes the action you assigned to 0 - in this    #
# example it echos your input to the terminal window.                         #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=`kdialog --inputbox "MESSAGE";`;

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with a single-line message that asks you #
# to type in some input. Command substitution is used to save your input to a #
# variable. The script then tests the Kdialog command's return to determine   #
# which action to take based on the input you entered or whether you entered  #
# any at all.                                                                 #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or the OK button is pressed. #
# In that case, the script executes the action you assigned to 0 - in this    #
# example it echos your input to the terminal window.                         #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --inputbox "MESSAGE";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with a two-line message (created by      #
# using an HTML <br> tag) that asks you to type in some input. Command        #
# substitution is used to save your input to a variable. The script then      #
# tests the Kdialog command's return to determine which action to take based  #
# on the input you entered or whether you entered any at all.                 #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or the OK button is pressed. #
# In that case, the script executes the action you assigned to 0 - in this    #
# example it echos your input to the terminal window.                         #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --inputbox "LINE1<br>LINE2";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 4:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window (with the EXAMPLE title) with a          #
# single-line message that asks you to type in some input. Command            #
# substitution is used to save your input to a variable. The script then      #
# tests the Kdialog command's return to determine which action to take based  #
# on the input you entered or whether you entered any at all.                 #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or the OK button is pressed. #
# In that case, the script executes the action you assigned to 0 - in this    #
# example it echos your input to the terminal window.                         #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --inputbox "MESSAGE" --title "EXAMPLE";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 5:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with a single-line message that asks you #
# to type in some input. Command substitution is used to save your input to a #
# variable. The script then tests the Kdialog command's return to determine   #
# which action to take based on the input you entered or whether you entered  #
# any at all.                                                                 #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or the OK button is pressed. #
# In that case, the script executes the action you assigned to 0 - in this    #
# example it echos your input to the file named FILENAME in the current       #
# directory, creating it if it doesn't exist and overwriting it if it does.   #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --inputbox "MESSAGE";);

if [ "$?" = 0 ]; then
	echo "$variable" > FILENAME;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 6:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with a single-line message that asks you #
# to type in some input. Example input is provided in the input box. Command  #
# substitution is used to save your input to a variable. The script then      #
# tests the Kdialog command's return to determine which action to take based  #
# on the input you entered or whether you entered any at all.                 #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or the OK button is pressed. #
# In that case, the script executes the action you assigned to 0 - in this    #
# example it echos your input to the terminal window unless you don't provide #
# any, in which case it echos DEFAULT to the terminal window,                 #
#                                                                             #
# The Kdialog command returns 1 if the Esc key or Cancel button is pressed.   #
# In that case, the script takes the action in you assigned to 1 - in this    #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --inputbox "MESSAGE" "DEFAULT";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Menu

These examples display a Kdialog window with a menu of choices. You get to pick one of the choices to trigger a specific message to be displayed in the terminal window. In the examples below, upper case alphabet letters and numbers are used to identify each menu item, but you can use lower case alphabet letters, words, or a combination of letters and numbers. Whatever you use will be what the script looks for to decide what to do. Some of you may notice that all the menu item text below is in quotes. The quotes are optional and are only needed when there’s a space or a character that needs to be escaped, but it doesn’t do any harm (other than using up space and making a script slightly larger) to leave them in.
Example 1:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a menu of choices. When you make a choice, command substitution is     #
# used to save your choice to a variable that is used to determine which of  #
# the five possible messages to display in the terminal window based on      #
# which choice you make or whether you make one at all.                      #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this example, it     #
# echos a message to the terminal window.                                    #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. In that     #
# case, the menu bypasses the choices and takes the action in the else       #
# section - in this example it echos YOU CHOSE CANCEL to the terminal        #
# window.                                                                    #
##############################################################################

variable=`kdialog --menu "CHOOSE:" A "APPLES" B "BANANAS" C "COCONUTS";`;

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;
Example 2:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a menu of choices. When you make a choice, command substitution is     #
# used to save your choice to a variable that is used to determine which of  #
# the five possible messages to display in the terminal window based on      #
# which choice you make or whether you make one at all.                      #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this example, it     #
# echos a message to the terminal window.                                    #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. In that     #
# case, the menu bypasses the choices and takes the action in the else       #
# section - in this example it echos YOU CHOSE CANCEL to the terminal        #
# window.                                                                    #
##############################################################################

variable=$(kdialog --menu "CHOOSE:" A "APPLES" B "BANANAS" C "COCONUTS";);

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;
Example 3:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a two-line message (created by     #
# using an HTML <br> tag) that offers you a menu of choices. When you        #
# make a choice, command substitution is used to save your choice to a       #
# variable that is used to determine which of the five possible messages to  #
# display in the terminal window based on which choice you make or whether   #
# you make one at all.                                                       #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this example, it     #
# echos a message to the terminal window.                                    #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. In that     #
# case, the menu bypasses the choices and takes the action in the else       #
# section - in this example it echos YOU CHOSE CANCEL to the terminal        #
# window.                                                                    #
##############################################################################

variable=$(kdialog --menu "LINE1<BR>LINE2:" A "APPLES" B "BANANAS" C "COCONUTS";);

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;

Back to top


Menu – alternate method

These examples use the cat command inside of command substitution to make a menu that offers you however many customizable choices you specify. Instead of specifying the choices on the command line, you can put them into a text file (FILENAME for example) with two columns – the first column containing (in this example) the letters of the alphabet representing the choices and the second column containing the text of the choices:
A APPLES
B BANANAS
C COCONUTS

Then you can use any of the examples below to work with the text file. If the text file isn’t in the same directory as the script, provide the full path to the file instead of just the file name.

Example 1:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a menu of choices that are gotten by using command substitution to     #
# read the contents of a file (in this case I used FILENAME), and by using   #
# command substitution to determine what you choose, if anything, and        #
# whether there is some sort of error. The result is saved to a variable     #
# that is tested to determine which action to take - in this example it will #
# echo text to the terminal window.                                          #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this example, it     #
# echos text to the terminal window.                                         #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. In that     #
# case, the menu bypasses the choices and takes the action in the else       #
# section - in this example it echos YOU CHOSE CANCEL to the terminal        #
# window.                                                                    #
##############################################################################

variable=`kdialog --menu "CHOOSE:" $(cat FILENAME;);`;

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	elif [ "$?" = 2 ]; then
		echo "YOU CHOSE CANCEL";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;
Example 2:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a menu of choices that are gotten by using command substitution to     #
# read the contents of a file (in this example I used FILENAME).             #
#                                                                            #
# Command substitution is then used to determine what you choose, if         #
# anything, and whether there is some sort of error. The result is saved to  #
# a variable that is tested to determine which action to take - in this      #
# example it will echo text to the terminal window.                          #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this example, it     #
# echos text to the terminal window.                                         #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. In that     #
# case, the menu bypasses the choices and takes the action in the else       #
# section - in this example it echos YOU CHOSE CANCEL to the terminal        #
# window.                                                                    #
##############################################################################

variable=$(kdialog --menu "CHOOSE:" $(cat FILENAME;););

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	elif [ "$?" = 2 ]; then
		echo "YOU CHOSE CANCEL";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;
Example 3:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a menu of choices. Command substitution is used to read the contents   #
# of a file (in this case I used FILENAME) and save them to a variable.      #
# Command substitution is also used to determine what you choose, if         #
# anything, and whether there is some sort of error. The result is saved to  #
# a variable that is tested to determine which action to take - in this      #
# example it will echo text to the terminal window.                          #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this example, it     #
# echos text to the terminal window.                                         #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. In that     #
# case the menu bypasses the choices and takes the action in the else        #
# section - in this example it echos YOU CHOSE CANCEL to the terminal        #
# window.                                                                    #
##############################################################################

contents=`cat FILENAME;`;
variable=`kdialog --menu "CHOOSE:" $contents;`;

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;
Example 4:
#!/bin/bash
##############################################################################
# This script opens a Kdialog window with a single-line message that offers  #
# you a menu of choices. Command substitution is used to read the contents   #
# of a file (in this case I used FILENAME) and save them to a variable.      #
# Command substitution is also used to determine what you choose, if         #
# anything, and whether there is some sort of error. The result is saved to  #
# a variable that is tested to determine which action to take - in this      #
# example it will echo text to the terminal window.                          #
#                                                                            #
# The menu returns 0 if the Enter key or the OK button is pressed. In that   #
# case, the script uses the choices you gave it, or the fact that you didn't #
# make a choice, to determine which action to take - in this cas it echos    #
# text to the terminal window).                                              #
#                                                                            #
# The menu returns 1 if the Esc key or Cancel button is pressed. The menu    #
# then bypasses the choices and takes the action in the else section - in    #
# this case it echos YOU CHOSE CANCEL to the terminal window.                #
##############################################################################

contents=$(cat FILENAME;);
variable=$(kdialog --menu "CHOOSE:" $contents;);

if [ "$?" = 0 ]; then
	if [ "$variable" = A ]; then
		echo "YOU CHOSE A";
	elif [ "$variable" = B ]; then
		echo "YOU CHOSE B";
	elif [ "$variable" = C ]; then
		echo "YOU CHOSE C";
	else
		echo "ERROR";
	fi;
else
	echo "YOU CHOSE CANCEL";
fi;

Back to top


Message box – error

These examples display a message inside a Kdialog error message box window.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that displays your error message. The    #
# command always returns 0.                                                   #
###############################################################################

kdialog --error "MESSAGE";
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with the EXAMPLE title. The window       #
# displays your error message. The command always returns 0.                  #
###############################################################################

kdialog --error "MESSAGE" --title "EXAMPLE";
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that displays your error message. The    #
# HTML <br> tag is used to add a carriage return. The command always          #
# returns 0.                                                                  #
###############################################################################

kdialog --error "LINE1<br>LINE2";

Back to top


Message box – information

These examples display a message inside a Kdialog information message box window.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that displays your information message.  #
# The command always returns 0.                                               #
###############################################################################

kdialog --msgbox "MESSAGE";
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that displays your information message.  #
# The HTML <br> tag is used to add a carriage return. The command             #
# always returns 0.                                                           #
###############################################################################

kdialog --msgbox "LINE1<br>LINE2";
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with the EXAMPLE title. The window       #
# displays your information message. The command always returns 0.            #
###############################################################################

kdialog --title "EXAMPLE" --msgbox "MESSAGE";
Example 4:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that is attached to the current window.  #
# The Kdialog window displays your information message. The command always    #
# returns 0.                                                                  #
###############################################################################

kdialog ${WINDOWID:+--attach $WINDOWID} --msgbox "MESSAGE";

Back to top


Message box – sorry

These examples display a message inside a Kdialog sorry message box window.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that displays your message. The command  #
# always returns 0.                                                           #
###############################################################################

kdialog --sorry "MESSAGE";
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window that displays your message. The HTML     #
# <br> tag is used to add a carriage return.  The command always              #
# returns 0.                                                                  #
###############################################################################

kdialog --sorry "LINE1<br>LINE2";
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog window with the EXAMPLE title. The window       #
# displays your message. The command always returns 0.                        #
###############################################################################

kdialog --title "EXAMPLE" --sorry "MESSAGE";

Back to top


Passive pop-up

These examples display a message in a Kdialog passive pop-up window for a specified number of seconds.
Example 1:
#!/bin/bash
#############################################################################
# This script opens a Kdialog passive pop-up window that displays a         #
# single-line message for a specified number of seconds. The command always #
# returns 0.                                                                #
#                                                                           #
#############################################################################

kdialog --passivepopup "MESSAGE" 5;
Example 2:
#!/bin/bash
#############################################################################
# This script opens a Kdialog passive pop-up window that displays a         #
# two-line message (created by using an HTML <br> tag) for a                #
# specified number of seconds. The command always returns 0.                #
#                                                                           #
#############################################################################

kdialog --passivepopup "LINE1<br>LINE2" 5;
Example 3:
#!/bin/bash
#############################################################################
# This script opens a Kdialog passive pop-up window with the EXAMPLE title, #
# The window displays a single-line message for a specified number of       #
# seconds. The command always returns 0.                                    #
#                                                                           #
#############################################################################

kdialog --title "EXAMPLE" --passivepopup "MESSAGE" 5;

Back to top


Password

These examples display a Kdialog window that asks you to enter your password into the box. Once you’ve done so, the password is displayed in the terminal window.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog password input window with a single-line        #
# message that asks you to enter your password. Command substitution is used  #
# to store your response, if any, in a variable that is tested to determine   #
# which action to take.                                                       #
#                                                                             #
# The command returns nothing and inserts a blank line into the terminal      #
# window if nothing is entered into the box and the OK button or the Enter    #
# key is pressed.                                                             #
#                                                                             #
# The command returns 0 if input is entered into the box and the OK button or #
# the Enter key is pressed. It then executes the command specified for a      #
# return of 0 - in this example it echos the password to the terminal window. #
#                                                                             #
# The command returns 1 if the Cancel button or the Enter key is pressed. It  #
# then executes the command specified for a return of 1 - in this example it  #
# echos YOU CHOSE CANCEL to the terminal window.                              #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=`kdialog --password "Please enter your password:";`;

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog password input window with a single-line        #
# message that asks you to enter your password. Command substitution is used  #
# to store your response, if any, in a variable that is tested to determine   #
# which action to take.                                                       #
#                                                                             #
# The command returns nothing and inserts a blank line into the terminal      #
# window if nothing is entered into the box and the OK button or the Enter    #
# key is pressed.                                                             #
#                                                                             #
# The command returns 0 if input is entered into the box and the OK button or #
# the Enter key is pressed. It then executes the command specified for a      #
# return of 0 - in this example it echos the password to the terminal window. #
#                                                                             #
# The command returns 1 if the Cancel button or the Enter key is pressed. It  #
# then executes the command specified for a return of 1 - in this example it  #
# echos YOU CHOSE CANCEL to the terminal window.                              #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --password "Please enter your password:";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog password input window with a two-line message   #
# (created by using an HTML <br> tag) that asks you to enter your             #
# password. Command substitution is used to store your response, if any, in a #
# variable that is tested to determine which action to take.                  #
#                                                                             #
# The command returns nothing and inserts a blank line into the terminal      #
# window if nothing is entered into the box and the OK button or the Enter    #
# key is pressed.                                                             #
#                                                                             #
# The command returns 0 if input is entered into the box and the OK button or #
# the Enter key is pressed. It then executes the command specified for a      #
# return of 0 - in this example it echos the password to the terminal window. #
#                                                                             #
# The command returns 1 if the Cancel button or the Enter key is pressed. It  #
# then executes the command specified for a return of 1 - in this example it  #
# echos YOU CHOSE CANCEL to the terminal window.                              #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --password "LINE1<br>LINE2";);

if [ "$?" = 0 ]; then
	echo "$variable";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Radio list

These examples display a Kdialog window that displays a message that asks you to choose an item from a radio list, which results in a message being displayed in the terminal window.
Example 1:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line message    #
# that asks you to choose an item from a radio list. Command substitution   #
# is used to store the result of the choice you make in a variable that is  #
# tested to determine which action the script will take.                    #
#                                                                           #
# The Kdialog command returns 0 if the OK button or Enter key is pressed.   #
# The Kdialog command combined with the echo command following it returns   #
# 1, 2, or 3, depending on which item you highlighted when you pressed the  #
# OK button or the Enter key. In that case, the script takes the action you #
# assigned to 1, 2, or 3 - in this example it echos YOU CHOSE RED, or it    #
# echos YOU CHOSE BLUE or it echos YOU CHOSE GREEN to the terminal window.  #
# If anything else happens, the script takes the action you assigned to     #
# else - in this example it echos ERROR to the terminal window.             #
#                                                                           #
# The Kdialog command returns 1 if the CANCEL button is pressed or if it is #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# CANCEL to the terminal window.                                            #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=`kdialog --radiolist "CHOOSE:" 1 "RED" on  2  "BLUE" on 3 "GREEN" off;`;

if [ "$?" = 0 ]; then
	if [ "$variable" = 1 ]; then
		echo "YOU CHOSE RED";
	elif [ "$variable" = 2 ]; then
		echo "YOU CHOSE BLUE";
	elif [ "$variable" = 3 ]; then
		echo "YOU CHOSE GREEN";
	else
		echo "ERROR";
	fi;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line message    #
# that asks you to choose an item from a radio list. Command substitution   #
# is used to store the result of the choice you make in a variable that is  #
# tested to determine which action the script will take.                    #
#                                                                           #
# The Kdialog command returns 0 if the OK button or Enter key is pressed.   #
# The Kdialog command combined with the echo command following it returns   #
# 1, 2, or 3, depending on which item you highlighted when you pressed the  #
# OK button or the Enter key. In that case, the script takes the action you #
# assigned to 1, 2, or 3 - in this example it echos YOU CHOSE RED, or it    #
# echos YOU CHOSE BLUE or it echos YOU CHOSE GREEN to the terminal window.  #
# If anything else happens, the script takes the action you assigned to     #
# else - in this example it echos ERROR to the terminal window.             #
#                                                                           #
# The Kdialog command returns 1 if the CANCEL button is pressed or if it is #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# CANCEL to the terminal window.                                            #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=$(kdialog --radiolist "CHOOSE:" 1 "RED" on  2  "BLUE" on 3 "GREEN" off;);

if [ "$?" = 0 ]; then
	if [ "$variable" = 1 ]; then
		echo "YOU CHOSE RED";
	elif [ "$variable" = 2 ]; then
		echo "YOU CHOSE BLUE";
	elif [ "$variable" = 3 ]; then
		echo "YOU CHOSE GREEN";
	else
		echo "ERROR";
	fi;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a two-line message       #
# (created by using an HTML <br> tag) that asks you to choose an item       #
# from a radio list. Command substitution is used to store the result of    #
# the choice you make in a variable that is tested to determine which       #
# action the script will take.                                              #
#                                                                           #
# The Kdialog command returns 0 if the OK button or Enter key is pressed.   #
# The Kdialog command combined with the echo command following it returns   #
# 1, 2, or 3, depending on which item you highlighted when you pressed the  #
# OK button or the Enter key. In that case, the script takes the action you #
# assigned to 1, 2, or 3 - in this example it echos YOU CHOSE RED, or it    #
# echos YOU CHOSE BLUE or it echos YOU CHOSE GREEN to the terminal window.  #
# If anything else happens, the script takes the action you assigned to     #
# else - in this example it echos ERROR to the terminal window.             #
#                                                                           #
# The Kdialog command returns 1 if the CANCEL button is pressed or if it is #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# CANCEL to the terminal window.                                            #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=$(kdialog --radiolist "LINE1<br>LINE2" 1 "RED" on  2  "BLUE" on 3 "GREEN" off;);

if [ "$?" = 0 ]; then
	if [ "$variable" = 1 ]; then
		echo "YOU CHOSE RED";
	elif [ "$variable" = 2 ]; then
		echo "YOU CHOSE BLUE";
	elif [ "$variable" = 3 ]; then
		echo "YOU CHOSE GREEN";
	else
		echo "ERROR";
	fi;
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Text box

These examples display a Kdialog text box window that displays the contents of a text file. This command wants to find ibus on your system. Otherwise, the terminal window will display warning messages about ibus not being installed. The warning messages will not prevent the Kdialog window from opening or your script from running.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog text box window that displays the contents of   #
# the specified file (in this example it displays the FILENAME file) and      #
# offers and OK button for you to press when finished viewing the file.       #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or OK button is pressed. In  #
# that case, the script executes the action you assigned to 0 - in this       #
# example it does nothing.                                                    #
#                                                                             #
# The Kdialog command returns 1 if the Esc key is pressed. In that case, the  #
# script takes the action in you assigned to 1 - in this example it echos YOU #
# CLOSED THE WINDOW to the terminal window.                                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

kdialog --textbox FILENAME;

if [ "$?" = 0 ]; then
	:;
elif [ "$?" = 1 ]; then
	echo "YOU CLOSED THE WINDOW";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog text box window of the size specified in pixels #
# (in this example it's 400 x 300) that displays the contents of the          #
# specified file (in this example it displays the FILENAME file) and offers   #
# and OK button for you to press when finished viewing the file.              #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or OK button is pressed. In  #
# that case, the script executes the action you assigned to 0 - in this       #
# example it does nothing.                                                    #
#                                                                             #
# The Kdialog command returns 1 if the Esc key is pressed. In that case, the  #
# script takes the action in you assigned to 1 - in this example it echos YOU #
# CLOSED THE WINDOW to the terminal window.                                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

kdialog --textbox FILENAME 400 300;

if [ "$?" = 0 ]; then
	:;
elif [ "$?" = 1 ]; then
	echo "YOU CLOSED THE WINDOW";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog text box window (with the EXAMPLE title) that   #
# displays the contents of the specified file (in this example it displays    #
# the FILENAME file) and offers and OK button for you to press when finished  #
# viewing the file.                                                           #
#                                                                             #
# The Kdialog command returns 0 if the Enter key or OK button is pressed. In  #
# that case, the script executes the action you assigned to 0 - in this       #
# example it does nothing.                                                    #
#                                                                             #
# The Kdialog command returns 1 if the Esc key is pressed. In that case, the  #
# script takes the action in you assigned to 1 - in this example it echos YOU #
# CLOSED THE WINDOW to the terminal window.                                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

kdialog --textbox FILENAME --title "EXAMPLE";

if [ "$?" = 0 ]; then
	:;
elif [ "$?" = 1 ]; then
	echo "YOU CLOSED THE WINDOW";
else
	echo "ERROR";
fi;

Back to top


Warning continue or cancel

These examples display a Kdialog window that displays a warning message and asks you to make a continue or cancel choice which results in a message being displayed in the terminal window.
Example 1:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line warning    #
# message that asks you to choose yes, no, or cancel. Command substitution  #
# is used to store the result of the choice you make, if any, in a variable #
# that is tested to determine which action the script will take.            #
#                                                                           #
# The Kdialog command returns 0 if the Continue button or the Enter key is  #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE CONTINUE to the terminal window.          #
#                                                                           #
# The Kdialog command returns 2 if the CANCEL button is pressed or if it is #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# CANCEL to the terminal window.                                            #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=`kdialog --warningcontinuecancel "MESSAGE";echo "$?";`;

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE CONTINUE";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line warning    #
# message that asks you to choose yes, no, or cancel. Command substitution  #
# is used to store the result of the choice you make, if any, in a variable #
# that is tested to determine which action the script will take.            #
#                                                                           #
# The Kdialog command returns 0 if the Continue button or the Enter key is  #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE CONTINUE to the terminal window.          #
#                                                                           #
# The Kdialog command returns 2 if the CANCEL button is pressed or if it is #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# CANCEL to the terminal window.                                            #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=$(kdialog --warningcontinuecancel "MESSAGE";echo "$?";);

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE CONTINUE";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a two-line warning       #
# message (created by using an HTML <br> tag) that asks you to choose       #
# yes, no, or cancel. Command substitution is used to store the result of   #
# the choice you make, if any, in a variable that is tested to determine    #
# which action the script will take.                                        #
#                                                                           #
# The Kdialog command returns 0 if the Continue button or the Enter key is  #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE CONTINUE to the terminal window.          #
#                                                                           #
# The Kdialog command returns 2 if the CANCEL button is pressed or if it is #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# CANCEL to the terminal window.                                            #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=$(kdialog --warningcontinuecancel "LINE1<br>LINE2";echo "$?";);

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE CONTINUE";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Warning yes or no

These examples display a Kdialog window that displays a warning message and asks you to make a yes or no choice which results in a message being displayed in the terminal window.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog warning yes no window with a single-line        #
# warning message that asks you to choose the Yes or No button. Your response #
# is used to determine which action to take.                                  #
#                                                                             #
# The command returns 0 if the Yes button is pressed or if the Yes button is  #
# selected and the Enter key is pressed. The script then executes the command #
# you specified for a return of 0 - in this example it echos YOU CHOSE YES to #
# the terminal window.                                                        #
#                                                                             #
# The command returns 1 if the Enter key is pressed without making a          #
# selection, if the No button is pressed, or if the No button is selected and #
# the Enter key is pressed. It then executes the command you specified for a  #
# return of 1 - in this example it echos YOU CHOSE NO to the terminal window. #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

kdialog --warningyesno "CHOOSE:";

if [ "$?" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE NO";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog warning yes no window with a two-line warning   #
# message (created by using an HTML <br> tag) that asks you to choose         #
# the Yes or No button. Your response is used to determine which action to    #
# take.                                                                       #
#                                                                             #
# The command returns 0 if the Yes button is pressed or if the Yes button is  #
# selected and the Enter key is pressed. The script then executes the command #
# you specified for a return of 0 - in this example it echos YOU CHOSE YES to #
# the terminal window.                                                        #
#                                                                             #
# The command returns 1 if the Enter key is pressed without making a          #
# selection, if the No button is pressed, or if the No button is selected and #
# the Enter key is pressed. It then executes the command you specified for a  #
# return of 1 - in this example it echos YOU CHOSE NO to the terminal window. #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

kdialog --warningyesno "LINE1<br>LINE2";

if [ "$?" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE NO";
else
	echo "ERROR";
fi;

Back to top


Warning yes or no or cancel

These examples display a Kdialog window that displays a warning message and asks you to make a yes, no, or cancel choice which results in a message being displayed in the terminal window.
Example 1:
#!/bin/bash
###############################################################################
# This script opens a Kdialog warning yes no window with a single-line        #
# warning message that asks you to choose the Yes or No button. Your response #
# is used to determine which action to take. Command substitution is used to  #
# save your choice to a variable that is used to determine which of the four  #
# messages to display in the terminal window based on which choice you make   #
# or whether you make one at all.                                             #                                                      
#                                                                             #
# The command returns 0 if the Enter key is pressed without making a          #
# selection, if th Yes button is pressed, or if the Yes button is selected    #
# and the Enter key is pressed. The script then executes the command you      #
# specified for a return of 0 - in this example it echos YOU CHOSE YES to the #
# terminal window.                                                            #
#                                                                             #
# The command returns 1 if the No button is pressed, or if the No button is   #
# selected and the Enter key is pressed. The script then executes the command #
# you specified for a return of 1 - in this example it echos YOU CHOSE NO to  #
# the terminal window.                                                        #
#                                                                             #
# The command returns 2 if the Cancel button or the Esc key is pressed. The   #
# script then executes the command you specified for a return of 2 - in this  #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=`kdialog --warningyesnocancel "MESSAGE";echo "$?";`;

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
###############################################################################
# This script opens a Kdialog warning yes no window with a single-line        #
# warning message that asks you to choose the Yes or No button. Your response #
# is used to determine which action to take. Command substitution is used to  #
# save your choice to a variable that is used to determine which of the four  #
# messages to display in the terminal window based on which choice you make   #
# or whether you make one at all.                                             #
#                                                                             #
# The command returns 0 if the Enter key is pressed without making a          #
# selection, if th Yes button is pressed, or if the Yes button is selected    #
# and the Enter key is pressed. The script then executes the command you      #
# specified for a return of 0 - in this example it echos YOU CHOSE YES to the #
# terminal window.                                                            #
#                                                                             #
# The command returns 1 if the No button is pressed, or if the No button is   #
# selected and the Enter key is pressed. The script then executes the command #
# you specified for a return of 1 - in this example it echos YOU CHOSE NO to  #
# the terminal window.                                                        #
#                                                                             #
# The command returns 2 if the Cancel button or the Esc key is pressed. The   #
# script then executes the command you specified for a return of 2 - in this  #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --warningyesnocancel "MESSAGE";echo "$?";);

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
###############################################################################
# This script opens a Kdialog warning yes no window with a two-line warning   #
# warning message (created by using an HTML <br> tag) that asks you to        #
# choose the Yes or No button. Your response is used to determine which       #
# action to take. Command substitution is used to save your choice to a       #
# variable that is used to determine which of the four messages to display in #
# the terminal window based on which choice you make or whether you make one  #
# at all.                                                                     #                                                           
#                                                                             #
# The command returns 0 if the Enter key is pressed without making a          #
# selection, if th Yes button is pressed, or if the Yes button is selected    #
# and the Enter key is pressed. The script then executes the command you      #
# specified for a return of 0 - in this example it echos YOU CHOSE YES to the #
# terminal window.                                                            #
#                                                                             #
# The command returns 1 if the No button is pressed, or if the No button is   #
# selected and the Enter key is pressed. The script then executes the command #
# you specified for a return of 1 - in this example it echos YOU CHOSE NO to  #
# the terminal window.                                                        #
#                                                                             #
# The command returns 2 if the Cancel button or the Esc key is pressed. The   #
# script then executes the command you specified for a return of 2 - in this  #
# example it echos YOU CHOSE CANCEL to the terminal window.                   #
#                                                                             #
# If anything else happens, the script executes the action you assigned to    #
# the else section - in this example it echos ERROR to the terminal window.   #
###############################################################################

variable=$(kdialog --warningyesnocancel "LINE1<br>LINE2";echo "$?";);

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


Yes or no

These examples display a Kdialog window that lets you make a yes or no choice which results in a message being displayed in the terminal window.
Example 1:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window with a single-line message that asks   #
# you to choose yes or no. The result of the choice you make, if any,       #
# determines which action the script will take.                             #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
# else - in this example it echos ERROR to the terminal window.             #
#############################################################################

kdialog --yesno "YES OR NO?";

if [ "$?" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$?" = 1 ]; 	then
	echo "YOU CHOSE NO";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window with a single-line message that asks   #
# you to choose yes or no. The result of the choice you make, if any,       #
# determines which action the script will take. The window has been given   #
# the QUESTION title.                                                       #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
# else - in this example it echos ERROR to the terminal window.             #
#############################################################################

kdialog --title "QUESTION" --yesno "YES OR NO?";

if [ "$?" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$?" = 1 ]; then
	echo "YOU CHOSE NO";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window with a two-line message (created by    #
# using an HTML <br> tag) that asks you to choose yes or no. The            #
# result of the choice you make, if any, determines which action the script #
# will take.                                                                #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

kdialog --yesno "LINE1<br>LINE2";

if [ "$?" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$?" = 1 ]; 	then
	echo "YOU CHOSE NO";
else
	echo "ERROR";
fi;

Back to top


Yes or no or cancel

These examples display a Kdialog window that lets you make a yes, no, or cancel choice which results in a message being displayed in the terminal window.
Example 1:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line message    #
# that asks you to choose yes, no, or cancel. The result of the choice you  #
# make, if any, determines which action the script will take.               #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# The Kdialog command returns 2 if the Esc key or Cancel button is pressed. #
# In that case, the menu bypasses the choices and takes the action in the   #
# else section - in this example it echos YOU CHOSE CANCEL to the terminal  #
# window.                                                                   #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

kdialog --yesnocancel "CHOOSE:";

variable="$?";

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 2:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a two-line message       #
# (created by using an HTML <br> tag) that asks you to choose yes,          #
# no, or cancel. The result of the choice you make, if any, determines      #
# which action the script will take.                                        #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# The Kdialog command returns 2 if the Esc key or Cancel button is pressed. #
# In that case, the menu bypasses the choices and takes the action in the   #
# else section - in this example it echos YOU CHOSE CANCEL to the terminal  #
# window.                                                                   #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

kdialog --yesnocancel "LINE1<br>LINE2";

variable="$?";

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 3:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line message    #
# that asks you to choose yes, no, or cancel. It uses command substitution  #
# to put the result into a variable that is tested to determine which       #
# action to take.                                                           #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# The Kdialog command returns 2 if the Esc key or Cancel button is pressed. #
# In that case, the menu bypasses the choices and takes the action in the   #
# else section - in this example it echos YOU CHOSE CANCEL to the terminal  #
# window.                                                                   #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=`kdialog --yesnocancel "CHOOSE:"; echo "$?";`;

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;
Example 4:
#!/bin/bash
#############################################################################
# This script opens a Kdialog window that displays a single-line message    #
# that asks you to choose yes, no, or cancel. It uses command substitution  #
# to put the result into a variable that is tested to determine which       #
# action to take.                                                           #
#                                                                           #
# The Kdialog command returns 0 if the Enter key or the Yes button is       #
# pressed. In that case, the script takes the action you assigned to 0 - in #
# this example it echos YOU CHOSE YES to the terminal window.               #
#                                                                           #
# The Kdialog command returns 1 if the No button is pressed or if it is     #
# highlighted while the Enter key is pressed. In that case, the script      #
# takes the action you assigned to 1 - in this example it echos YOU CHOSE   #
# NO to the terminal window.                                                #
#                                                                           #
# The Kdialog command returns 2 if the Esc key or Cancel button is pressed. #
# In that case, the menu bypasses the choices and takes the action in the   #
# else section - in this example it echos YOU CHOSE CANCEL to the terminal  #
# window.                                                                   #
#                                                                           #
# If anything else happens, the script takes the action you assigned to     #
#  else - in this example it echos ERROR to the terminal window.            #
#############################################################################

variable=$(kdialog --yesnocancel "CHOOSE:"; echo "$?";);

if [ "$variable" = 0 ]; then
	echo "YOU CHOSE YES";
elif [ "$variable" = 1 ]; then
	echo "YOU CHOSE NO";
elif [ "$variable" = 2 ]; then
	echo "YOU CHOSE CANCEL";
else
	echo "ERROR";
fi;

Back to top


See also:

The KDE TechBase tutorial is an excellent place to learn about Kdialog.




Obligatory Happy Ending

And they all lived happily ever after. The end.

Back to top

4 Comments »

  1. With regard to your first line you can also do this.
    With a file which has two (maybe more but not tested) columns you can, pipe the file though kdialog –menu “Select a language:”

    New command line.

    kdialog –menu “select your course: ” $(cat ClassesAvailable) –geometry 700×400

    where the ClassesAvailable file contains:

    a French
    b English
    c German
    d Chinese

    You get the same result.

    Comment by ompaul — April 5, 2011 @ 9:08 am

    • Thanks! I’ve added it above under Menu (alternate method). :)

      Comment by mostlylinux — April 6, 2011 @ 12:23 pm

  2. How do you get the list and coloum option available in zenity under kdialog. Kdialog looks really great but there are no proper tutorial (with example) available on the internet.

    I am writing a bash script with GUI both zenity and kdialog so as to avoid no dependency error. I have completed the zenity part sucessfully but struggling in kdialog, especially in list and coloum (available in zenity). If you know more about kdialog please share with us.

    Thanks and regards,
    sundar_ima

    Comment by sundar_ima — April 13, 2011 @ 4:23 pm

    • I think the closest thing KDialog has to Zenity’s list and column would be the –menu option shown at the top of this page. From everything I’ve been able to find out, KDialog is not as comprehensive as Zenity. Maybe the official KDE TechBase KDialog tutorial will help.

      Linux Magazine did a brief overview comparison of KDialog and Zenity that shows what each is capable of. Enjoy! :)

      Comment by mostlylinux — April 14, 2011 @ 1:00 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: WordPress Classic. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.