5

My bash script is

zenity --question --text=Continue? && echo Continuing...

How can I make it so it would echo Stopping if the user selected no? i.e.:

zenity --question --text=Continue? && echo Continuing... !&& echo Stopping...
t3hcakeman
  • 2,289
  • 4
  • 25
  • 27

5 Answers5

7

It's not really the opposite of &&, but something like this might do:

zenity --question --text=Continue? && echo Continuing... || echo Stopping...
zigdon
  • 14,573
  • 6
  • 35
  • 54
  • 4
    Please be cautious! If your "true"-statement isn't a simple `echo` and can return non-zero value, "else"-statement will be executed too! Please try `true && false || echo "Stopping..."` – uzsolt Nov 27 '11 at 19:10
3

It's the logical OR, ||:

zenity --question --text=Continue? || echo Continuing...

(So true && cmd, false || cmd and cmd all do the same thing.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

Use || to create an "OR list":

zenity --question --text=Continue? && echo Continuing... || echo Stopping...

See http://www.gnu.org/s/bash/manual/bash.html#Lists.

ruakh
  • 175,680
  • 26
  • 273
  • 307
0
if zenity --question --text='Continue?'
then echo Continuing...
else echo Stopping...
fi
zwol
  • 135,547
  • 38
  • 252
  • 361
0

There may be a way to do it on a single line but I usually use the following which I find a bit more readable:

if zenity --question --text=Continue?
then
        echo Continuing...
else
        echo Stopping...
fi
Simon C
  • 1,977
  • 11
  • 14