home

Quickies, by Andrea Olivato

text

Create a Zenity progress bar using wget output

I found this simple script to redirect wget progress bar to a nicer zenity window.

Sed is used to parse the output and send to Zenity only the correct progress. An infinite bash cycle is used to check that zenity is still running (if the user closes the window probably you want wget to be killed)

# Start wget | zenity
# Note the & at the end of the pipe, this allows the script to continue with wget running in the background
wget http://filename 2>&1 | sed -u 's/.*\ \([0-9]\+%\)\ \+\([0-9.]\+\ [KMB\/s]\+\)$/\1\n# Downloading \2/' | zenity --progress --title="Downloading File..." &

#Start a loop testing if zenity is running, and if not kill wget
RUNNING=0
while [ $RUNNING -eq 0 ]
do
if [ -z "$(pidof zenity)" ]
then
  pkill wget
  RUNNING=1
fi
done

2 years ago

July 9, 2009
Comments (View)

Zenity dialog collection

photo Zenity dialog collection

2 years ago

July 7, 2009
Comments (View)
text

Use Zenity calendar to get dates in different formats in bash.

Quick example on how to open a calendar with Zenity, get the selected date from a Bash script, and change the returned date format (timestamp f.e.).

#!/bin/bash

# Creates a Zenity calendar obj, 
# Put the selected date on a variable.

DATE=`$(zenity --calendar --text "When to leave?" --title "Holidays"); echo $szDate`;

# If you want to return the date in a different format you can play with
# the --date-format parameter.
# The syntax is the same of date command.

# This shows how to get the timestamp
TIMESTAMP=`$(zenity --calendar --text "When to leave?" --title "Holidays" --date-format=%s); echo $szDate`

# If you want to show as first view an other month/year you can specify it with
# --day, --month and --year parameters.

# This shows how to set the date to july 2007
TIMESTAMP=`$(zenity --calendar --text "When to leave?" --title "Holidays" --day 10 --month 7 --year 2010); echo $szDate`

# Obviously you can always do whatever you want with returned values.
echo ${DATE}" - "${TIMESTAMP};

2 years ago

July 7, 2009
Comments (View)
text

Zenity dialog. Get the answer from Bash.

This shows how to create a Zenity dialog window and how to get the answer given by user into a Bash variable.

#!/bin/bash

# Creates a Zenity question dialog, 
# similar to JS confirm().
# Put the answer code on a variable.

ANSWER=$(zenity --entry --text "What's your name?" ); echo $szAnswer

# Creates a Zenity popup (information dialog)
# and put there user previous answer

zenity --info --text "Hello ${ANSWER}";

# On the questions dialog you can even set a
# text to be shown on the text field.
# This is like HTML value="" proprierty

ANSWER=$(zenity --entry --text "What's your name?" --entry-text "Type your name"); echo $szAnswer

2 years ago

July 7, 2009
Comments (View)
text

Zenity questions

Quick examples on how to launch Zenity questions from Bash and use the returned values.

#!/bin/bash

# Creates a Zenity question dialog, 
# similar to JS confirm().
# Put the answer code on a variable.

ANSWER=`zenity --question --text "Do you like me?"; echo $?`;

# Check what the user pressed
if [[ $ANSWER -eq 1 ]]
then
	# 1 => pressed cancel
	echo "No, you don't";
else
	# 0 => pressed ok
	echo "Oh yes, you do";
fi;

2 years ago

July 6, 2009
Comments (View)