Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Note this is run on a debian linux environment

I have the following sample script called HelloWorld.sh

#!/bin/bash

echo "Hello World"
exit 1

I then run the following command

script -c ./HelloWorld.sh foo ; echo $?

$? returns 0 which is expected since the script command itself ran successfully. However, I want the return code HelloWorld.sh which would be 1 if I just ran the following command.

bash HelloWorld.sh ; echo $?

Looking at the man page, I see that the script command doesn't return anything so the following wouldn't help either

return_code=`script -c ./HelloWorld.sh foo`

The question I would like to ask is whether there is a way that I can get the return value of HelloWorld.sh when running it in the context of the script command such as sending the exit value to the parent shell. If not, how would you recommend doing it?

My possible answers on how I would do it if I can't use the script command

  1. Have HelloWorld.sh write to shared memory using the shm API
  2. Have HelloWorld.sh write to a file

Both methods require cleanup of the shared memory object and the file respectively, so I am asking for my own personal knowledge whether there is a simpler solution

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

From the manual page of script

 -e, --return
         Return the exit code of the child process.  Uses the same format
         as bash termination on signal termination exit code is 128+n.

So this should work

$ script -e -c ./HelloWorld.sh; echo $?
Script started, file is typescript
Hello World
Script done, file is typescript
1
share|improve this answer
    
Thanks for the example. It appears that I should have clarified in my question that the man page I was looking at was from a centos 6 system which didn't have that option. I will check the server tomorrow for the option. –  Jason Zhu Oct 16 '13 at 1:16
add comment

man script,

   -e, --return
          Return the exit code of the child process.  Uses the same format
          as bash termination on signal termination exit code is 128+n.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.