Page 1 of 1

is there a reliable way to grab exit codes when su? SOLVED

Posted: Tue Nov 18, 2014 5:38 pm
by GDixon
su -c command really messes with exit codes.
I've tried getting them all ways from sunday using a case esac menu and failed. What happens is the last command that runs is what returns a exit code.
From my limited comprehension it's caused by running the command as su . Besides not running the command as su which is not an option is there a way to do it reliably?

I'm trying to grab the exit codes for freshclam 0 is ran correctly and updated / it outputs a 58 if the update fails due to no connection to the update site with a internet connection / and a 1 exit is a fail when i use the wrong password for su

I can get all the correct codes running the command all alone in a shell and then using echo $? after i do each one.
I get a 0 when it updates
I get a 1 when i use the wrong password
I get a 58 when i make the update fail by using a temporary gibberish update site
the 58 exit code prints to screen the site is off line unavailable or no connection

su -c freshclam is the command

I've even tried out putting the exit codes to a tmp file and then importing them back to the shell after su closes but I must not be doing that right since it has yet to work.
There has to be an easier way?
maybe it's time to learn more than just bash?

i'm really hating su -c now :)

In the past i have ran something requiring root as sudo with no password but I learned thats a security problem waiting to happen. Just how bad is it to run something as sudo like that?

Re: is there a reliable way to grab exit codes when su? SOLV

Posted: Tue Nov 25, 2014 7:55 pm
by GDixon
Found a way but is going to take some testing and tweaking to make it work.

I would like to know for sure the update worked or not by exit codes which i think will be more accurate and complete.

I'm finding case esac pretty versatile

Code: Select all

     case "$?" in

         0|1|58)

             case "$?" in

                   0) Update OK   # exit was ok make a log entry
                      ;;

                   1) Wrong Password
                      ;;     

                  58) exit code prints to screen the site is
                      off line unavailable or no connection
                      ;;

                    *) Unknown error for now
 
             esac