Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
RANGE=4
number=$RANDOM
let "number %= $RANGE"

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

if ($number == 0); then
    COMMENT=Comment0
elif ($number == 1); then
    COMMENT=Comment1
elif ($number == 2); then
    COMMENT=Comment2
elif ($number == 3); then
    COMMENT=Comment3

echo $COMMENT

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.

Thanks

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

2 Answers 2

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

#!/bin/bash

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

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

if [ $number = 0 ]; then
    COMMENT="Comment0"
elif [ $number = 1 ]; then
    COMMENT="Comment1"
elif [ $number = 2 ]; then
    COMMENT="Comment2"
elif [ $number = 3 ]; then
    COMMENT="Comment3"
fi

echo $COMMENT

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:

#!/bin/bash

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

 
discard

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.