return status of commands run in su - <name> -c "<command>"

return status of commands run in su - <name> -c "<command>"

Post by Ted » Fri, 20 Sep 2002 04:52:25



Hi

I was wondering how you can get the return status of a command which is run
within the context of su - <name> -c "<command>"

I am trying to find out the status of my command, but it always returns 0

su - dbsa -c "
$DB_DIR/dbinfo
echo $?
"

even if the dbinfo command fails, it will print out 0. Is there any way to
get the actual return status?

Note if I "su - dbsa" on the command line, run the command and have it fail,
it will return a non-zero return code.

Thanks

Ted

 
 
 

return status of commands run in su - <name> -c "<command>"

Post by James Kan » Fri, 20 Sep 2002 05:13:40


I guess your "su -c" command returns "0" as exit code becuase it does
not get Nonzero fail code from the "dbinfo" and the "$?" is just exit
code of "su -c" not the "dbinfo".
You should look for another way to find out exit code of "dbinfo".
Maybe a short shell script which will definitely get "$?" right as
your "su - dbsa" does.

James


> Hi

> I was wondering how you can get the return status of a command which is run
> within the context of su - <name> -c "<command>"

> I am trying to find out the status of my command, but it always returns 0

> su - dbsa -c "
> $DB_DIR/dbinfo
> echo $?
> "

> even if the dbinfo command fails, it will print out 0. Is there any way to
> get the actual return status?

> Note if I "su - dbsa" on the command line, run the command and have it fail,
> it will return a non-zero return code.

> Thanks

> Ted


 
 
 

return status of commands run in su - <name> -c "<command>"

Post by Barry Margoli » Fri, 20 Sep 2002 05:09:27




>I am trying to find out the status of my command, but it always returns 0

>su - dbsa -c "
>$DB_DIR/dbinfo
>echo $?
>"

>even if the dbinfo command fails, it will print out 0. Is there any way to
>get the actual return status?

Your problem is with quoting, not su.

su - dbsa -c "$DB_DIR/dbinfo;"'echo $?'

The first command needs to be in double quotes so that $DB_DIR will be
expanded by the original shell; the second is in single quotes so that $?
won't be -- it will then be expanded by the subshell that su runs.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

return status of commands run in su - <name> -c "<command>"

Post by Joe Blogg » Fri, 20 Sep 2002 05:26:17



Quote:> Hi

> I was wondering how you can get the return status of a command which is
run
> within the context of su - <name> -c "<command>"

> I am trying to find out the status of my command, but it always returns 0

> su - dbsa -c "
> $DB_DIR/dbinfo
> echo $?
> "

> even if the dbinfo command fails, it will print out 0. Is there any way to
> get the actual return status?

> Note if I "su - dbsa" on the command line, run the command and have it
fail,
> it will return a non-zero return code.

> Thanks

> Ted

Use sudo.  It passes the resultcode back as you want.
 
 
 

return status of commands run in su - <name> -c "<command>"

Post by Ted » Fri, 20 Sep 2002 08:56:09


Thanks that worked,

It made me realize the error of my ways...

You could also backslash the $ within the quotes if you wanted it in the
command. (ie if you wanted a number of lines within the quotes)

ie
su - dbsa -c "
$DB_DIR/dbinfo
echo \$?
"

later

ted




> >I am trying to find out the status of my command, but it always returns 0

> >su - dbsa -c "
> >$DB_DIR/dbinfo
> >echo $?
> >"

> >even if the dbinfo command fails, it will print out 0. Is there any way
to
> >get the actual return status?

> Your problem is with quoting, not su.

> su - dbsa -c "$DB_DIR/dbinfo;"'echo $?'

> The first command needs to be in double quotes so that $DB_DIR will be
> expanded by the original shell; the second is in single quotes so that $?
> won't be -- it will then be expanded by the subshell that su runs.

> --

> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.
 
 
 

return status of commands run in su - <name> -c "<command>"

Post by Ted » Fri, 20 Sep 2002 09:00:22


Thanks,  that worked.

Quote:> su - dbsa -c "$DB_DIR/dbinfo;"'echo $?'

> The first command needs to be in double quotes so that $DB_DIR will be
> expanded by the original shell; the second is in single quotes so that $?
> won't be -- it will then be expanded by the subshell that su runs.

It made me rember what I was doing wrong.

the following will also work if you need to have multi lines in the su
command

su - dbsa -c "
$DB_DIR/dbinfo
echo \$?
"

Thanks again...

Ted

 
 
 

1. >>>Compiling 2.0 problem: "asm86: Command not found" <<<

I got this when the compilation enters the final state (compiling the zImage)

as86 -0 -a -o bootsect.o bootsect.s
make[1]: as86: Command not found
make[1]: *** [bootsect.o] Error 127
make[1]: Leaving directory '/usr/src/linux-2.0/arch/i386/boot'
make: *** [zImage] Error 2

Please e-mail (don't post).

Your help is greatly appreciated.

Lawrence

2. meaning of this error message on boot?

3. <><><> MOUNTING EXTENDED PARTITION <><><>

4. Permissions for DOS mounted on /dosc

5. Wanted: <><><> Unix Specialist <><><>

6. Can't cut-n-paste

7. LILO help <><><><><><>

8. RH6.0 and Epson 500 printer

9. <font color="blue"><B>kmail</B></font> question

10. <font color="blue"><B>modemblaster</B></font>

11. Are there any command-line alternatives to the <ctrl> <alt> <+> ?

12. rsh <hostname> -n -l <user> <command> hangs on Solaris 8

13. <Alt>+<key> = <Esc><key> ?