Development/Tutorials/Shell Scripting with KDE Dialogs

< Development‎ | Tutorials
Revision as of 02:08, 12 January 2007 by CuCullin (Talk | contribs) (Created Page)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
This section needs improvements: Please help us to

cleanup confusing sections and fix sections which contain a todo

Introduction and Scope

There are some misconceptions that KDE is only a graphical environment. While it is true that KDE is an outstanding desktop environment, the Unix heritage of command line and scripting is also well supported by KDE. In particular, KDE applications can be controlled from the command line, and shell scripts can make use of some of the KDE widget set.

To use this tutorial, you'll need to have some basic familiarity with command line fundamentals, and be at least aware of shell scripting. Like any other programming environment, effective shell scripting requires solid knowledge of the environment. However, you should be able to make sense of the examples with only basic understanding. The downside to this is that if you are very familiar with shell scripting, some of the explanation is likely to be redundant.

This tutorial assumes that you are using the GNU bash shell, or something directly compatible. Users of other shells (especially csh and variants) may need to modify the examples.

Shell scripting techniques and usage varies a lot. Sometimes a script is only meant to be run by the system (e.g. as cron job), and other times scripts are really applications intended to be run by users. KDE includes features that allow you to use some KDE functionality from a shell script, which can save work, and can also make your script feel like it is part of a nicely integrated application set.

As an example, consider something like a password dialog. If you need a user to enter a password, you can easily generate a dialog from your script that looks like the following:

Shell Scripting with KDE Dialogs-Password Dialog.png

kdialog Usage

The key to using KDE dialogs in shell scripts is an application named kdialog. To generate a password dialog as shown in above, 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 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

[watson@bakerst]$ kdialog --password "Some Text" hello [watson@bakerst]$ 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

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

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

Example 4: Password Dialog, with Return Value Check

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

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 echoed as shown in Example 2, unless you redirect it.

Example 5: Password Dialog Showing Redirection

[watson@bakerst]$ kdialog --password "Enter the password" > password.file [watson@bakerst]$ 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 English (British or American) layout keyboards, above the "7" key on French layout keyboards, and on the top right of German layout keyboards.

=== Example 6: Password Dialog Using a Shell Variable [watson@bakerst]$ password=`kdialog --password "Enter the password"` [watson@bakerst]$ echo $password Secreter

Example 7: Password Dialog with Title

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. kdialog --title "ACAP entry" --password "Please enter the server access code:"

Which results in:

Shell Scripting with KDE Dialogs-Password Dialog with Title.png