Gtk-server, tus bash scritps con interfaz gráfica
Gtk-server es un software con el cual podemos crear interfaces gráficas (GTK 1.x o 2.x) en un un montón de shell scripts, desde bash a Perl, pasando por Ruby, Awk o Python (aquí lista completa).
Gtk-server se compila como un binario al que haremos referencias en nuestros shell scripts y que proporciona una especie de API a las librerías GTK, así podremos desde nuestro fichero en texto plano llamar a GTK para dibujar ventanas en el escritorio.
A pensar de lo que se pueda pensar a primera vista, gtk-server no supone una limitación por si misma para programar interfaces gráficas utilizando Gtk, se pueden hacer cosas muy interesantes mediante este software.
A continuación, vamos a ver el famoso Hola Mundo! con bash y gtk-server (una ventana con un botón que pone “Hola Mundo” y si le das se cierra la venta):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/bin/bash ### ESTA PRIMERA PARTE ES PARA CREAR LAS FUNCIONES EMBEBIDAS DE GTK PARA BASH (.gtk4bash) # no es necesario utilizar esto antes, pero te quita trabajo, y es más intuitivo. ################################################################################################### # # Create embedded GTK functions for BASH # # (c) Peter van Eerten 2006, GPL license # # Tested with: # -BASH 3.0 on Slackware 10 and GTK-server 2.1.4 # # October 11, 2006: Initial release # October 12, 2006: Added check to see when created PIPE file is ready # January 16, 2007: Solved problem of unique filename # August 20, 2007: Improved for GTK-server 2.1.4 # October 13, 2007: Added mechanism for finding configfile # May 14, 2008: Added define function, code is even shorter now # Oct 9, 2008: Added default ‘gtk’ function to use after new definition of calls # # Put the first part of this file at the start of each of your BASH scripts and # you can embed GTK as if you are using the original API. #————————————————————————————————- # Set the extending globbing option in BASH shopt -s extglob # Pipe filename must be unique for your application PIPE=”/tmp/gtk.bash.\$$” # Find GTK-server configfile first if [[ -f gtk-server.cfg ]]; then CFG=gtk-server.cfg elif [[ -f /etc/gtk-server.cfg ]]; then CFG=/etc/gtk-server.cfg elif [[ -f /usr/local/etc/gtk-server.cfg ]]; then CFG=/usr/local/etc/gtk-server.cfg else echo “No GTK-server configfile found! Please install GTK-server…” exit 1 fi # Now create global functionnames from GTK API if [[ ! -f $HOME/.gtk4bash || $CFG -nt $HOME/.gtk4bash ]]; then echo “#!/bin/bash” > $HOME/.gtk4bash echo “gtk-server -fifo=$PIPE &” >> $HOME/.gtk4bash echo “while [ ! -p $PIPE ]; do continue; done” >> $HOME/.gtk4bash while read LINE do if [[ $LINE = FUNCTION_NAME* && $LINE = +(*gtk_*|*gdk_*|*g_*|*glade_*) ]]; then LINE=${LINE#*= } printf “\nfunction ${LINE%%,*}\n” >> $HOME/.gtk4bash printf “{\n/bin/echo ${LINE%%,*} \$@ > $PIPE” >> $HOME/.gtk4bash printf “\nread GTK < $PIPE\n}\n” >> $HOME/.gtk4bash fi done < $CFG printf “\nfunction gtk()\n{\necho \$1 > $PIPE; read GTK < $PIPE;\n}\n” >> $HOME/.gtk4bash fi # Declare global variables declare GTK NULL=”NULL” unset CFG PIPE LINE # Assignment function function define() { $2 $3 $4 $5 $6 $7 $8 $9; eval $1=”$GTK”; } # Wait for user read -p “Press <enter> to start hola mundo…” VAR #————————————————————————————————- # Aquí empieza el Hola Mundo #————————————————————————————————- # Include the generated ‘.gtk4bash’-file in the shellscript to use embedded GTK functions . $HOME/.gtk4bash # Definir GUI gtk_init “NULL NULL” define WINDOW gtk_window_new 0 gtk_window_set_title $WINDOW “‘Hola Mundo!’” gtk_window_set_position $WINDOW 1 gtk_window_set_default_size $WINDOW 230 150 define BUTTON gtk_button_new_with_label “‘Hola Mundo’” gtk_widget_set_size_request $BUTTON 50 25 gtk_container_add $WINDOW $BUTTON gtk_widget_show_all $WINDOW # Inicializar variables EVENT=0 # Mainloop while [[ $EVENT != $BUTTON && $EVENT != $WINDOW ]] do define EVENT gtk_server_callback “wait” done # Exit GTK gtk_server_exit |
Este es un pequeño ejemplo de lo que se puede hacer con gtk-server y bash (u otros lenguajes de script), aquí y aquí se pueden encontrar más ejemplos de lo que se puede hacer, y aquí encontrarás algunas aplicaciones creadas con gtk-server.
Gtk-server no es una aplicación para construir de manera fácil y rápida aplicaciones gráficas (como podría ser Zenity), tienes que conocer GTK, ya que lo único que hace es portar este a los lenguajes de script.
Para empezar a utilizarlo puedes descargarlo desde su web aquí.
Si descargas el source (wget http://downloads.sourceforge.net/gtk-server/gtk-server-2.3.1-sr.tar.gz) lo tienes que descomprimir:
1 2 3 | $ tar xzvf gtk-server-2.3.1-sr $ cd gtk-server-2.3.1-sr |
Y compilarlo e instalarlo:
1 2 3 4 5 | $ ./configure $ make # make install |
Y ya puedes empezar a crear tus scrip con interfaz gráfica. Para ello tienes que leer la documentación que puedes encontrar aquí.
Próximamente colgaré un pequeño manual de iniciación para utilizar Gtk-server y Bash.

Comentarios recientes