2

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
2

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.

  • it gives me script: invalid option -- 'e' – Rahul Aug 7 '16 at 5:58
  • @Rahul exactly what command did you type? 'script -e' or 'script --return' should work on Linux. What OS are you using? – John Leuenhagen Aug 7 '16 at 6:01
  • I am using debian squeeze. I tried both commands both gives me same error. – Rahul Aug 7 '16 at 6:06
  • @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 Aug 7 '16 at 6:34
1

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)
$
  • it should be incorporated in script – Rahul Aug 7 '16 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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