0

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.

2 Answers 2

1

"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.

0

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.