kdialog Usage

The key to using KDE dialogs in shell scripts is an application named kdialog. To generate a password dialog as shown in Figure 1 in the Section called Introduction and Scope, you could use the following command line.

Example 1. Password dialog

kdialog --password "Please enter the server access code:"

Let's look at the code in Example 1 in a bit more detail. The arguments to kdialog are used to control the type of dialog that is produced and the parameter or parameters of that dialog box. In the case of the password dialog, you use --password to specify the dialog type, and then follow that with the parameter, which is the text that appears in the dialog box.

Each time you run kdialog (or any other application), there is a return value that indicates whether the application ran as expected, or failed in some way. You can access this return value as $?, as shown in the following example.

Example 2. Shell script return values

[bradh@rachel bradh]$ kdialog --password "Some Text"
hello
[bradh@rachel bradh]$ echo $?
0

Note

The $? variable is updated when each foreground process exits. If you need to use that variable later, you need to save it away.

In this example, the return value is zero. It would be one if the Cancel button had been selected instead of the OK button. The convention is that negative numbers indicate failure, however the shell normally subtracts them from 256. This means that if you fail to specify a required argument, the system returns -2, and $? returns 254.

Note

This is different to the convention used by the underlying widgets. If you are familiar with the underlying Qt widgets, this might be a bit confusing, however it is important to conform to the standard approach to shell scripting.

Example 3. Shell script return value, with error

[bradh@rachel bradh]$ kdialog --password
kdialog: '<text>' missing.
kdialog: Use --help to get a list of available command line options.
[bradh@rachel bradh]$ echo $?
254

In a shell script, you might choose to test the return value after each invokation.

Example 4. Password dialog, with return value check

    kdialog --password "Please enter the server access code:"
    if [ $? = 0 ]; then
	    echo " you selected: OK"
    else
	    echo " you selected: Cancel"
    fi

In addition to the return value, you also get the password itself (assuming that you selected OK). After all, what is the point of a password dialog unless you can use the result? For the password dialog, and other kdialog dialogs that provide input capabilities, the output is sent to standard output. This allows you to redirect the input to a file, or pipe it to another program. In the case of the password dialog, the text that is entered will be echo'd as shown in Example 2, unless you redirect it.

Example 5. Password dialog, showing redirection

$ kdialog --password "Enter the password" > password.file
$ cat password.file
Secrter

Instead of saving the result in a file, you can also use a shell variable. Note that you need to use the "backtick" notation - this key is normally found on the top left of the keyboard.

Example 6. Password dialog, using a shell variable

[bradh@rachel kdialog]$ password=`kdialog --password "Enter the password"`
[bradh@rachel kdialog]$ echo $password
Secreter

While not shown in the previous examples, you can also use the --title option to specify the title of the dialog box, as shown in the following example.

Example 7. Password dialog, with title

kdialog --title "ACAP entry" --password "Please enter the server access code:"

The result of such a usage is shown below.

Figure 2. Password dialog example, including a window title