Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The script command logs an entire shell session to file. This is great, but when reviewing the session I'd like to be able to see the exit code of each command that I ran.

Is it possible to capture this in the script logs? Ideally without displaying it to the user of the script session, but that would be acceptable if necessary.

Example of running script:

me:~ oliverlade$ script
Script started, output file is typescript
bash-3.2$ echo "hello"
hello
bash-3.2$ true
bash-3.2$ false
bash-3.2$ exit
exit

Script done, output file is typescript

The contents of the log file:

me:~ oliverlade$ cat typescript
Script started on Sun Aug  7 13:02:43 2016
bash-3.2$ echo "hello"
hello
bash-3.2$ true
bash-3.2$ false
bash-3.2$ exit
exit

Script done on Sun Aug  7 13:03:03 2016

What I'd like it to be:

me:~ oliverlade$ cat typescript
Script started on Sun Aug  7 13:02:43 2016
bash-3.2$ echo "hello"
hello
(exit 0)
bash-3.2$ true
(exit 0)
bash-3.2$ false
(exit 1)
bash-3.2$ exit
exit

Script done on Sun Aug  7 13:03:03 2016
share|improve this question

From the script man pages:

-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." I misinterpreted this...

If you need to use script, it seems as though the best way to see the last exit code would be to add it in your shell prompt. It's not a bad idea to do this anyways.

share|improve this answer
    
it gives me script: invalid option -- 'e' – Rahul yesterday
    
@Rahul exactly what command did you type? 'script -e' or 'script --return' should work on Linux. What OS are you using? – John Leuenhagen yesterday
    
I am using debian squeeze. I tried both commands both gives me same error. – Rahul yesterday
    
@Rahul quite strange, here is the debian man pages for script: goo.gl/6h7Dzy , you can see the options there. I recommend adding the exit code to your bash prompt. This way, it will be logged, and you can see it in the shell as well. – John Leuenhagen yesterday

You may incorporate the exit status of the last executed command ($?) in your primary prompt (PS1).

One example:

$ PS1='$? \$ '
0 $

Testing it:

0 $ false
1 $

You have to use single quotes to avoid expanding $? at the time of assignment to PS1. You should not export the prompt variable.

Another example:

$ PS1=$'(exit $?)\n$ '
(exit 0)
$

Testing that:

(exit 0)
$ false
(exit 1)
$
share|improve this answer
    
it should be incorporated in script – Rahul yesterday

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.