Exit Status within sudo su - $user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit Status within sudo su - $user
# 1  
Old 07-24-2008
Exit Status within sudo su - $user

I am having a problem reading the exit status of a command or script within a sudo.


Code:
return_code=99

sudo su - $User << EOF >> $output_file
$Script
return_code=$? 
exit $return_code
EOF

echo "return_code=$return_code"

returns
"return_code=99"

the $script above is a test script with a "exit 5" at the end. When I run it as the user and echo $? I get a 5.

I have been scanning the forums, trying my own stuff too, but cant seem to find the answer. How do I get the return code from the script (or command) passed out?

Thanks if you can help.
# 2  
Old 07-24-2008
Quote:
Originally Posted by quigley007
I am having a problem reading the exit status of a command or script within a sudo.


Code:
return_code=99

sudo su - $User << EOF >> $output_file
$Script
return_code=$? 
exit $return_code
EOF

echo "return_code=$return_code"

returns
"return_code=99"

the $script above is a test script with a "exit 5" at the end. When I run it as the user and echo $? I get a 5.

I have been scanning the forums, trying my own stuff too, but cant seem to find the answer. How do I get the return code from the script (or command) passed out?

Thanks if you can help.
You need to echo it, or stick it in a file, or shove it down a pipe, or send it to syslog, or use it as padding in a ping... point is, $return_code is going to be the "return_code" variable of the shell that launched the sudo, and never the "return_code" variable of the shell you launched via sudo. You need to pass it some other way than as a shell variable.

You can test by doing this:

Code:
return_code="Luke, I am your father."

sudo su - $User << EOF >> $output_file
$Script
return_code=$? 
exit $return_code
EOF

echo "return_code=$return_code"

And sure enough, you'll get "return_code=Luke, I am your father."
# 3  
Old 07-24-2008
One of the things I tried was sticking it in a file, and reading it from the file:
Code:
sudo su - $User << EOF >> $output_file
echo 99 > $output_error_file1
$Script
echo "$?" > $output_error_file1
chmod 644 $output_error_file1
EOF

cat $output_error_file1

I still recieve a "0" ..

I am not sure what I am doing wrong here... I need the $? from $Script ... I am not grasping some concept I am sure of that, and once I get it I will understand what you are getting at.
# 4  
Old 07-27-2008
Quote:
Originally Posted by quigley007
One of the things I tried was sticking it in a file, and reading it from the file:
Code:
sudo su - $User << EOF >> $output_file
echo 99 > $output_error_file1
$Script
echo "$?" > $output_error_file1
chmod 644 $output_error_file1
EOF

cat $output_error_file1

I still recieve a "0" ..

I am not sure what I am doing wrong here... I need the $? from $Script ... I am not grasping some concept I am sure of that, and once I get it I will understand what you are getting at.
If you're getting 0 back from the script you mentioned above, then you *are* getting the return value back from inside the su, since otherwise it'd be 99, no?
# 5  
Old 07-28-2008
Here is the script I am calling :

Code:
#!/usr/bin/ksh
#
echo "hi..."
exit 5

When I run it as the user it echos hi... and returns a 5. the only exit status it seems to be capturing is the 0 from the su.
# 6  
Old 07-29-2008
You are still not capturing the exit code correctly. Try this.

Code:
sudo su - $User << EOF >> $output_file
$Script
EOF

If your example is incomplete, and you really do need to store $? somewhere temporarily, you need to quote it inside the here document, because as you originally wrote it, it will be expanded to the exit code when the here document is read and evaluated, not to the exit code from $Script.
# 7  
Old 07-29-2008
Hmm, really? Admittedly, this is just on a Cygwin box I have handy, but...
Code:
angry-chipmunk:~$ FOO=bar
angry-chipmunk:~$ echo $FOO
bar
angry-chipmunk:~$ cat <<"EOF"
> Hello, everyone
> This is $FOO.
> EOF
Hello, everyone
This is $FOO.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

2. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

3. UNIX for Dummies Questions & Answers

Processes and exit status

Hi, I can't understand why the last $? is 1? can somebody plz help me to understand it? thanks $ ksh $ ps -f UID PID PPID C STIME TTY TIME COMMAND msarabad 12361 12319 0 15:17:58 pts/1 0:00 ksh msarabad 12319 12317 0 15:15:11 pts/1 0:00 -sh msarabad 12362 12361 ... (7 Replies)
Discussion started by: messi777
7 Replies

4. UNIX for Dummies Questions & Answers

service exit status

what are the number for the exit status for command service and what does every number mean. (2 Replies)
Discussion started by: programAngel
2 Replies

5. UNIX for Dummies Questions & Answers

$? = Exit status variable

hi, exit status variable $?, returns some digits. 0 ---> succes. 1..126 Failure (the program itself will decide what the numbers mean) 127 Command not found 128..254 The program did not exit normally. (E.g., it crashed, or received a signal) 255 Invalid exit code well, if $?... (4 Replies)
Discussion started by: dummydba
4 Replies

6. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

7. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

8. UNIX for Dummies Questions & Answers

exit status conditions

Hi all, i am writing a script to test if some servers are down and prompt if test positive. i used rlogin and rsh then exit but the script when run, logs into the servers and stays. pls what can i do to salvage this? or what other options do you suggest? (6 Replies)
Discussion started by: sdcoms
6 Replies

9. Shell Programming and Scripting

Checking Exit Status

I hope one of you smart people out there can help me with what seems like a real simple questing but I can't quite figure out. In a script I am doing a cmp on two files. I am trying to check the exit status with an if statement but can't seem to figure out the syntax. If the exit status is 1 I... (4 Replies)
Discussion started by: PrimeRibAndADew
4 Replies

10. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies
Login or Register to Ask a Question