How do I know if user cancelled?

Asked by David D Lowe

If a user cancels gksu, how can I retrieve that information programmatically? The exit code stays 0.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gksu Edit question
Assignee:
No assignee Edit question
Solved by:
Harvey Muller
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Harvey Muller (hlmuller) said :
#1

David,

According to the gksu manpage, the following should be reported:

"On success, gksu will return 0. If an authentication error ocurred, it will exit with error code 3. If the user canceled the dialog or closed the window, it will return error code 2. On other error conditions, gksu will return 1."

Therefore, if the user cancels the dialog or closes the window, gksu "should" return error code 2. If it is not doing this, then something is probably wrong.

To retrieve the error code using a shell script sh or bash), then here is a small shell script sample:

    #!/bin/sh

    gksu gedit || echo "gksu gedit erred with code: $?"

The first part "gksu gedit" runs gedit as the superuser. If it errors out, then the second part runs. '||' is the equivalant of 'or'. The second part prints the error message, '$?' is the status, which should be '1' or '2'.

Please return back here to change the status of the question to Solved, if this solves your question.

Thanks!

Harvey

Revision history for this message
David D Lowe (flimm) said :
#2

That's not in the man page of the gksu I have.

If I run your script and cancel, nothing is printed.
If I run this script using sh

#!/bin/sh
gksu gedit
echo "gksu gedit erred with cod: $?"

I get:
gksu gedit erred with code: 0

Even when I click cancel.
The package of gksu I have installed is 2.0.0-5ubuntu3

Revision history for this message
Harvey Muller (hlmuller) said :
#3

Hi David,

I googled the previous answer, and have confirmed that on Hardy the gksu manpage does not state that. Apologize for that noise.

I'm testing on my ubuntu machine right now, and will report back shortly.

Harvey

Revision history for this message
Harvey Muller (hlmuller) said :
#4

David,

The gksu implementation used by Ubuntu does not appear to return any error codes. /var/log/auth.log certainly does not contain this information. I've checked other logs on the system and am unable to find related error messages that could be checked.

For what it's worth, here's a link displaying the information I provided in the first post:

    http://pwet.fr/man/linux/commandes/gksu

It looks like that at one time, gksu did emit error codes.

Best regards,

Harvey

Revision history for this message
David D Lowe (flimm) said :
#5

Thanks Harvey Muller, that solved my question.

Revision history for this message
David D Lowe (flimm) said :
#6

Thanks for that info. So there's no easy way to find out if gksu cancelled?

Revision history for this message
Best Harvey Muller (hlmuller) said :
#7

David,

There appears to be no way to return the status of an application which was started by gksu. Based on the manpage I found above, it looks like at one time, at least one version of gksu did.

Best regards,

Harvey

Revision history for this message
David D Lowe (flimm) said :
#8

Thanks Harvey Muller, that solved my question.