Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using the linux 'script' command http://www.linuxcommand.org/man_pages/script1.html to log all input and output in an interactive bash script.

At the moment I have to call the script command, then run my bash script, then exit.

I want to run the script and exit commands from within the actual bash script itself. How can I do this?

I've tried script -a but that doesn't work for interactive scripts.

Any assistance would be greatly appreciated.

share|improve this question

2 Answers 2

"script" forks a shell (or a command specified with "-c"), so I don't think you can call "script" from within the script you want to "script".

Take this (annoyingly) interactive shell script (called "fred"):

#!/bin/bash

while read -p 'Pete and Repeat were on a boat. Pete jumped off. Who was left? ' WHO
do
        :
done

Run it and "script" the interaction to a file:

[myles@marklar ~]$ script -c ~/fred fred.log
Script started, file is fred.log
Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat
Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat
Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat
Pete and Repeat were on a boat. Pete jumped off. Who was left? Script done, file is fred.log

The log file contains:

[myles@marklar ~]$ cat fred.log
Script started on Mon 12 Nov 2012 11:44:41 PM EST
Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat
Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat
Pete and Repeat were on a boat. Pete jumped off. Who was left? Repeat
Pete and Repeat were on a boat. Pete jumped off. Who was left?
Script done on Mon 12 Nov 2012 11:44:51 PM EST

In short, you'll have to call your interactive shell script with the "script" command, as far as I know.

share|improve this answer

You can trivially redirect all output of your script to a file using the following construct:

exec &> /your/script.log

In addition, bash allows you to log all commands it executes, all input it receives, and all expansions it performs when you add the following:

set -x

If these are the first two lines of your script, everything that the script is doing will be logged.

share|improve this answer

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.