Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Given a file that was output by script, is there a way to run all of those commands in order, and then check to see if the output matches what was in the script file?

I assume that diff can be used for the last part, but I have no idea on how to do the first. Any ideas?

Using a tcsh shell on linux (Debian, but I doubt it matters).

share|improve this question
The answer is going to be yes regardless, but some environmental information would help you get a better answer. Window? Unix? What shell? – EBGreen Feb 23 '12 at 19:00
@EBGreen, see edit – soandos Feb 23 '12 at 19:02

Edit: From the manual:

Script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.

This means it might be possible to do in the simplest cases, by looking for occurrences of $PS1 in the output and running the string after it, but it could soon become impossible:

  • You can't see the difference between a literal character and an escape character like ^C.
  • Any background jobs can insert text at any time, messing up the output. while true; do sleep $RANDOM; echo garbage; done &
  • If it records standard error, there's the whole asynchronous output issue.

If it's at all an option, I'd rather use /var/log/cmdlog, and see if all of the text produced by the commands recorded there occur anywhere within the script file.

share|improve this answer
I do not want to redo all the work I have already done, hence the first part of the question. How can I get the commands out of the script file? – soandos Feb 24 '12 at 16:18
Copy and paste. It should run just as if you'd run the original script, only in the different context. – l0b0 Feb 24 '12 at 17:04
No, as every keypress (even up arrow) is recorded, and that messes up the terminal. In addition, it would run the output of the commands, which is also something I don't want – soandos Feb 24 '12 at 18:43
If it's an interactive application, you're probably better off using expect. But this does not run the output of the commands - (command) is different from $(command). – l0b0 Feb 24 '12 at 20:47
When I say script I mean the command script not some script I wrote. Your answer does not seem to be aware of this – soandos Feb 24 '12 at 21:38

Okay, so one of the semantics of diff is that it outputs text if the files differ, and outputs nothing if they are the same.

Given files foo and bar, here's what you can do:

# Run your script
if [ "$(diff foo bar)" = "" ]; then
    echo "They are the same"
    echo "They are different"

(You're first part isn't exactly clear - do you have a sample output out.template and script > out.log and want to make sure that out.template and out.log are the same? Or something else?)

share|improve this answer
It is not a script in the sense that it is some script that can run. It is a script in the sense that it is the output of the script command. How can I run it? – soandos Feb 24 '12 at 0:47
@soandos Not easily - it dumps everything from VT100 color escapes to output from ncurses programs. (The manpage states that this is not what the naive user expects). – new123456 Feb 24 '12 at 5:06

Your Answer


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.