Operating System - HP-UX
1835085 Members
2606 Online
110073 Solutions
New Discussion

Re: HP-UX 11 shell script: getting return code from embedded command

 
Bob Swail
Occasional Contributor

HP-UX 11 shell script: getting return code from embedded command

I've set up the sudo utility on our HP-UX box to allow certain logins to add/modify/delete user accounts on the system. Basically sudo allows you to specify one or more commands that a given user can run as root.

I want to know how I can get the return code of a command that is embedded within the sudo command. For example:

$sudo logins -l zinzan
>>user zinzan does not exist
$echo $?
>>0

It seems that the return code 0 I get is for the sudo command and not the logins. Is there a way to isolate the return code of the embedded command?

Thanks
Bob Swail
5 REPLIES 5
Paula J Frazer-Campbell
Honored Contributor

Re: HP-UX 11 shell script: getting return code from embedded command

Bob

Not 100% sure but sudo should be handling this return code and a sudo switch might give you the code.

I do not have sudo or a server at present to check it.

Something like this.

$sudo - logins -l zinzan


Paula




If you can spell SysAdmin then you is one - anon
Paula J Frazer-Campbell
Honored Contributor

Re: HP-UX 11 shell script: getting return code from embedded command

Bob

RETURN VALUES
Upon successful execution of a program, the return value from sudo will simply be the return value of the program that was executed.

Otherwise, sudo quits with an exit value of 1 if there is a configuration/permission problem or if sudo cannot execute the given command. In the latter case the error string is printed to stderr. If sudo cannot stat(2) one or more entries in the user's PATH an error is printed on stderr. (If the directory does not exist or if it is not really a directory, the entry is ignored and no error is printed.) This should not happen under normal circumstances. The most common reason for stat(2) to return ``permission denied'' is if you are running an automounter and one of the directories in your PATH is on a machine that is currently unreachable.


Manual here:-

http://www.courtesan.com/sudo/man/sudo.html#return_values

Paula
If you can spell SysAdmin then you is one - anon
Bob Swail
Occasional Contributor

Re: HP-UX 11 shell script: getting return code from embedded command

Thanks for the reply. While sudo claims that the return code is that of the executed command, it appears that it's not the case.

$login root

#logins -l zinzan
>>login 'zinzan' not found
#echo $?
>>253

#login bswail

$sudo logins -l zinzan
>>login 'zinzan' not found
#echo $?
>>0

If sudo was returning the return code of the logins command, I would expect to see 253 as is tha case when running logins as root.

Bob
Paula J Frazer-Campbell
Honored Contributor

Re: HP-UX 11 shell script: getting return code from embedded command

Bob

The sudo return code you are seeing is the return code of the "sudo 'login'" command itself.

It is not thr return code of "logins -l zinzan".

A code of 0 indicated that sudo was able to run "logins".

Sudu is a pseudo shell and as such you are dealing with two levels of return codes.

try this:-

create a script say test.sudo

#!/bin/sh
logins -| zinzan
echo $? >

give it ececute permissions, then

$sudo test.sudo
$echo $?


the echo in the script should return the error code from the login command and the echo from the command line the return code of 0 to indicate that sudo ran logins.

HTH

Paula



If you can spell SysAdmin then you is one - anon
Bob Swail
Occasional Contributor

Re: HP-UX 11 shell script: getting return code from embedded command

Paula

No go.

$cat test.sudo
>>logins -l zinzan
>>echo $?

$sudo test.sudo
>>login 'zinzan' not found
>>0

$echo $?
>>0

Seems like we're still getting the return code from the sudo command and not logins.

Thanks for your help anyway!

Bob