Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having a problem writing a script for 'zenity.' My intention is to have a comment selected randomly from a list and then displayed on the screen for the user to see. To be honest, I would like to have the program run its self automatically every two hours or so, but I don't have a clue how to make that happen. I am new to bash scripting and am simply trying to teach it to myself through Google searches, the bash scripting guide, and trial and error. This program will, in the end, generate a random stupid remark every couple of hours; purely for comic relief.

#! /bin/bash

#Create a random number between 0 and 3
let "number %= $RANGE"

echo $number
#Set random number generated above to select from list of comments.

if ($number == 0); then
elif ($number == 1); then
elif ($number == 2); then
elif ($number == 3); then


zenity --warning --title='!!!WARNING!!!' text=$COMMENT

Anyway, I am having a lot of trouble with the 'if' statements. From what I have learned so far, it looks right to me; but I get nothing but errors despite many attempted edits. I would sure like some help from anyone interested.


share|improve this question
You might find it useful to look at the programs fortune (which displays random quotes, and you could write a custom quote file for), and notify-send which displays a message in a notification bubble rather than a dialog box). – chronitis Oct 29 '13 at 14:14

The script is almost good. But have a problem with brackets when using if. These must be square brackets ([ ... ]):


#Create a random number between 0 and 3
let number%=$RANGE

echo $number
#Set random number generated above to select from list of comments.

if [ $number = 0 ]; then
elif [ $number = 1 ]; then
elif [ $number = 2 ]; then
elif [ $number = 3 ]; then


zenity --warning --title="!!!WARNING!!!" --text="$COMMENT"

See also the others small modifications.

More about:

share|improve this answer

I'd go for a slightly different setup, using an array:


comments[1]="This is the first comment"
comments[2]="This is the second comment"
comments[3]="This is the third comment"
comments[4]="This is the last comment"

zenity --warning --title="Raa" --text="${comments[ 1 + RANDOM % ${#comments[@]}]]}"

The last line is where all the clever stuff happens. ${#comments[@]} represents the number of elements in the comments array so we're finding a number between 0 and that number (0-3) and adding 1.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.