请选择 进入手机版 | 继续访问电脑版

技术控

    今日:35| 主题:61447
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Adding a Simple GUI to shell scripts with kdialog

[复制链接]
高跟鞋的旋律 发表于 2016-12-1 04:39:18
230 2
Adding a Simple GUI to Linux shell scripts with kdialog

   Shell scripts are incredibly useful things. They allow you to do something as basic as creating an easy command to replace a more difficult one with lots of flags, to batching up many complex commands to run from acron job. They’re great because you can quickly fire them off in your favorite terminal, but in some cases they require you to remember specific combinations of flags or options. If you find yourself in this situation, you can add some simple GUI dialogs to help you speed your way through the task at hand.
  In order to make use of this tutorial, you’ll need the following:
  
       
  • Access to a Unix-like shell (this is written with bash in mind).
       
  • macOS and *nix OSes (like Linux) should come with this or one like it.
       
  • On Windows, you could install the CygWin environment, or on recent versions of Windows 10 use the "Windows Subsystem for Linux"
       
  •   The base libraries for the KDE desktop environment. You don’t need the entire thing, especially if you already have another DE installed.
      
   Installing kdialog should only be necessary if you started with a desktop environment other than KDE, and you have no other KDE applications on your machine. It is a part of the package kde-baseapps-bin on Ubuntu, which means it is installed by default on Kubuntu. On other derivatives, you can install it with the following command at the terminal:
  Installing kdialog from the terminal.

  1. sudo apt-get install kde-baseapps-bin
复制代码
This will likely result in a sizable installation, as other KDE libraries and packages will be installed along with it. But hey, hard drive space is cheap, right?
  Since scripts are (in general) supposed to do things automatically, we’ll only need to show this dialog in the following situations:
  
       
  • If we need to capture some sort of input from the user, or;
       
  • If we want to show the user some sort of feedback in a noticable way (i.e. not just some text spat out at the command-line).
      
  Let’s take the example of using a command to [backup your Drupal site using drush]. Of course, since you’re a budding Internet entrepreneur, you don’t just have one Drupal site—? you have three. You’d rather not have to run separate commands when you want to back-up all your sites, but at the same time you don’t want to have to remember a long path if you want to take a snapshot of just one. We can use a couple of dialogs to help ourselves by:
  
       
  • Present the user with a list of sites we can back-up, and as her to make a choice, and;
       
  • Show the user a message confirming the result, whether it’s a success or failure.
      
   kDialog works by calling it at the command line along with a dialog type , any parameters that type may require, and any other options such as a dialog title.
  There are a good variety of dialog types to choose from depending on your needs, as follows:
        kDialog Types              Dialog Type     Sample Image     Description                    Information Message Boxes
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-1-技术控-difficult,following,something,situation,remember
       Displays a box with an "OK" button to close, providing a text message and either a "non-critical" information, warning, or error icon.
               Passive Pop-up
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-2-技术控-difficult,following,something,situation,remember
       This displays a message that will disappear within a few seconds, i.e. it doesn’t require the user to close or dismiss it.
               Yes/No Message Box
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-3-技术控-difficult,following,something,situation,remember
       Like the "Information Message Boxes," this displays a dialog with an information/warning/error icon that will also provide the user’s selection of "Yes" or "No."
               Input Dialog Boxes
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-4-技术控-difficult,following,something,situation,remember
       This provides a single box into which the user can type some text.
               File Display Boxes
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-5-技术控-difficult,following,something,situation,remember
       This will display the contents of a (plain text) file within a dialog box.
               Menu and Selection Dialog Boxes
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-6-技术控-difficult,following,something,situation,remember
       This provides a dialog with a list of items, allowing the user to make one or more selections:
      
          
  • The Menu and Radiolist Dialogs allow selection of one option.
          
  • The Checklist Dialog allows selection of one, multiple, or all options.
          
  • The Combobox Dialog allows the selection of one option using a drop-down menu.
          
               File Selection Dialog Boxes
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-7-技术控-difficult,following,something,situation,remember
       The user can select part of the filesystem, including:
      
          
  • Select existing files, with the ability to filter by name or filetype
          
  • Enter a new of a new file to be saved/updated
          
  • Directories
          
               Progress Dialogs
      

Adding a Simple GUI to shell scripts with kdialog

Adding a Simple GUI to shell scripts with kdialog-8-技术控-difficult,following,something,situation,remember
       The user can combine kDialog with the dcop command to create a dialog that updates the user on the progress of an operation.
            Looking over the above options, two of them jump out as having potential:
  
       
  • The "Checklist Dialog" will allow us to select one, or all, of our sites to back-up.
       
  • The "Information Message Box" can be used to display whether each of these has succeeded or failed.
      
  Given these options, we can imagine our script looking something like this:
  1. SITE1 is located HERE (make these full paths, not relative paths)
  2. SITE2 is located THERE
  3. SITE3 is located IN ANOTHER PLACE

  4. display a kDialog with SITE1, SITE2, and SITE3 as options
  5.     and tell me which one(s) the user picks

  6. for each one the user picked:
  7.     run +drush ard+ on that site, and
  8.     record whether it was successful or not.

  9. display another +kDialog+ with each of those results.
复制代码
With that in mind, let’s dive right in.
  The first thing we should do is assemble the drush command we want to apply to each site when it is selected. Suppose all the sites are on the same web host in different directories, but we want to store the back-ups in a common "backup" directory. We can use the following:
  1. cd SITE
  2. drush ard --destination=/home/user/backup/SITENAME.tar.gz
复制代码
  So using your knowledge ofsetting variables andfor loops, we can make this script as follows:
  1. #! /bin/bash

  2. // Note: create variables to hold paths to all the sites
  3. PERSONALSITE='/home/www/mypersonalsite/'
  4. BUSINESSSITE='/home/www/mybusinesssite/'
  5. HOBBYSITE='/home/www/myhobbysite/'

  6. // Note: create variable to hold path to the destination
  7. DESTINATION='/home/user/backup'

  8. // Note: display a dialog asking the user to select which sites to back-up
  9. CHOICES=$(kdialog --checklist "Select sites to back-up:" 1 "Personal site" off 2 "Business Site" off 3 "Hobby site" off)

  10. // Note: log a success/failure message for each of the choices returned from dialog
  11. for each in $CHOICES
  12. do
  13.     case {
  14.         1)
  15.             cd $PERSONALSITE
  16.             drush ard --destination=$DESTINATION/mypersonalsite.tar.gz
  17.             if [$?=="0"]
  18.             then
  19.                 RESULTS += "Personal site backup: Succeeded\n"
  20.             else
  21.                 RESULTS += "Personal site backup: Failed\n"
  22.             fi
  23.         2)
  24.             cd $BUSINESSSITE
  25.             drush ard --destination=$DESTINATION/mybusinesssite.tar.gz
  26.             if [$?=="0"]
  27.             then
  28.                 RESULTS += "Business site backup: Succeeded\n"
  29.             else
  30.                 RESULTS += "Business site backup: Failed\n"
  31.             fi
  32.         3)
  33.             cd $HOBBYSITE
  34.             drush ard --destination=$DESTINATION/myhobbysite.tar.gz
  35.             if [$?=="0"]
  36.             then
  37.                 RESULTS += "Hobby site backup: Succeeded\n"
  38.             else
  39.                 RESULTS += "Hobby site backup: Failed\n"
  40.             fi
  41.     esac

  42. // NOTE: display a dialog with the results of all the backup operations.
  43. kdialog --msgbox $RESULTS
复制代码
  If we save this and make we can run it as described in the first article in the Bash series we’ll be able to run a single command, get an easy-to-use dialog to choose our site, then see a nice window reporting back how everything went.
  While this script can certainly be optimized in a couple of ways, it demonstrates how you can use kdialog to collect some user input and display some results by running a single program within your script.
947068972 发表于 2016-12-1 07:49:18
一觉醒来,天都黑了。
回复 支持 反对

使用道具 举报

TuKi321 发表于 2016-12-28 08:36:51
为何要放弃治疗?
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表