You are not logged in.

#1 2011-08-29 19:50:27

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

[SOLVED] Help on Bash dialog prompt script needed

I need a script to invoke a window and ask yes/no question, after that this script should run "reboot" or "halt" depending on the answer.
I've downloaded gxmessage and wrote this small script

#!/bin/bash
answer=$(gxmessage  "Are you sure you want to shutdown? " -buttons yes,no -print) &&
if [$answer == "yes"]; 
then exec sys_halt
else exit
fi

But it doesn't seem to work, tip me where am I wrong?

Last edited by tasty_minerals (2011-08-29 21:05:42)


lenovo thinkpad EDGE 13'

Offline

#2 2011-08-29 19:53:16

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] Help on Bash dialog prompt script needed

[ is not part of if's syntax. 'if' expects commands, and [ is a command with "$answer", "=", and "yes" being arguments to it. You don't type "lsdir", you type "ls dir".

if [[ "$answer" = "yes" ]]; then
  # do stuff
else
  # do something else
fi

Note that usage of '==' isn't POSIX, meaning it's invalid inside [ ]. Bash will, however, accept this without whining (a bug, imo).

I suggest reading this: http://mywiki.wooledge.org/BashGuide

Last edited by falconindy (2011-08-29 19:53:56)

Offline

#3 2011-08-29 19:56:56

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

Re: [SOLVED] Help on Bash dialog prompt script needed

falconindy wrote:

[ is not part of if's syntax. 'if' expects commands, and [ is a command with "$answer", "=", and "yes" being arguments to it. You don't type "lsdir", you type "ls dir".

if [[ "$answer" = "yes" ]]; then
  # do stuff
else
  # do something else
fi

Note that usage of '==' isn't POSIX, meaning it's invalid inside [ ]. Bash will, however, accept this without whining (a bug, imo).

I suggest reading this: http://mywiki.wooledge.org/BashGuide

thanks a lot


lenovo thinkpad EDGE 13'

Offline

#4 2011-08-29 20:03:36

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

Re: [SOLVED] Help on Bash dialog prompt script needed

well what I did is

#!/bin/bash
gxmessage  "Are you sure you want to shutdown? " -buttons yes,no &&
if [[ $? == 101 ]];
then
        exec sys_halt
else
        exit
fi

$? contains 101(yes) or 102(no), but no effect still,
could you please be more specific. sorry


lenovo thinkpad EDGE 13'

Offline

#5 2011-08-29 20:13:48

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] Help on Bash dialog prompt script needed

Non-zero exit values are false, so the second half of your compound command will never execute. Really, read the guide. It's very specific and very user friendly.

Offline

Board footer

Powered by FluxBB