Welcome to my site. Please CLICK HERE to give your opinions regarding this new look of "PCTipsbyAnu". Thanks for visiting.

Friday, October 15, 2010

Browse » Home » , , , , , , , , » LINUX : How to write GUI Script in BASH?

LINUX : How to write GUI Script in BASH?


When it comes to writing customapplications for servers or desktops on Linux, there are a number of choices oflanguages to use. You can write apps in python, perl, ruby, or even php, amongso many others. Scripts can be written in the shell as well, using bash, zsh,or tcsh, to name a few. Out of all of these, writing scripts in the shell (andbash comes with pretty much every Linux distribution out there), is perhaps theeasiest to pick up and the quickest to write.

The nice thing about a shell scriptis that, in itself, there isn’t much to do as far as programming. Shell scriptsare really just a sequence of commands you would write on the command line. Itdoes support if-then-else type statements; it supports loops; and it can becomequite complex if you want it to be, but it doesn’t have to be.


And, like other languages, shellscripts can have GUI front-ends as well. There are a few different choiceshere: Zenity for the GNOME desktop, kdialog for the KDE desktop, and dialog fora TUI, providing menus and prompts suitable for the CLI. To put the power ofshell scripts into a little perspective, I wrote the entire Annvix operatingsystem install program in bash, using dialog to ask questions about theinstallation.

With Zenity and kdialog, you canwrite scripts that prompt users for input using GUI windows. For instance, toask the user to answer a question with “Yes” or “No” you would use:
zenity--question "Do you wish to proceed?"
rc=$?
if[ "${rc}" == "0" ]; then
   answer="yes"
else
   answer="no"
fi
and the equivalent in kdialog:
kdialog--yesno "Do you wish to proceed?"
rc=$?
if[ "${rc}" == "0" ]; then
   answer="yes"
else
   answer="no"
fi

Both Zenity and kdialog will displayfurther information with the –help option, and Zenity has a manpage that can be referenced forhelp.

Another example is in creating aprogress bar. For instance, to show a progress bar while finding all backupfiles in your home directory, you might use:
find. -name '*~' | tee >(zenity --progress --pulsate --title "Findingbackup files" --text "Searching for backup files...")>out.txt
zenity--width 400 --height 100 --title "File list" --text-info--filename=out.txt


kdialog works a little strangelyhere, so the above example doesn’t work so well since find only works inone run, but to illustrate with a little sleep trickery:
ref=$(kdialog--progressbar "Finding backup files" 3)
qdbus${ref} org.kde.kdialog.ProgressDialog.setLabelText "Searching for backupfiles..."
qdbus${ref} Set org.kde.kdialog.ProgressDialog value 1
find. -name '*~' >out.txt
qdbus${ref} Set org.kde.kdialog.ProgressDialog value 2
sleep2
qdbus${ref} Set org.kde.kdialog.ProgressDialog value 3
qdbus${ref} org.kde.kdialog.ProgressDialog.close
kdialog--textbox out.txt 400 100
Both will display a progress bar(quite short lived, for this example) and then will display the output of thefind command (which was redirected to a file called out.txt). Note thatkdialog on KDE3 is different than it is on KDE4 (the above is for KDE4). If youare still using KDE3, communication is done via DCOP, rather than DBUS.
These illustrations are quitesimple, but they demonstrate the possibilities of what can be done with eithertool. Out of the two, I find Zenity to be much simpler to work with, but theyboth work quite well, and it of course depends upon your environment of choice.
GUI programming for shell scripts,with helper programs like Zenity and kdialog, is much easier to accomplish thanGUI programs for other programming environments. If you need to write a programthat requires user interaction, and you also need that interaction to be viathe desktop and not a terminal or shell, then these tools can help realize thatgoal.

CODE :
DOWNLOAD COMPLETE TUTORIAL  (Password : hack)


You can leave a response, or trackback from your own site.

About 'Anu': My name is 'Anu' also Known as 'ANU 007 TIGER' .I'm administrator of 'PC Tips by Anu' blog .This blog was opened for sharing contents about hacking n cracking.
Thanks YAHOO OR GMAIL

0 comments:

Post a Comment

 
Back to Top