-   Linux - Newbie (
-   -   how to capture or log outputs/message during script execution (

VijayaRaghavanLakshman 01-23-2012 10:33 AM

how to capture or log outputs/message during script execution
Hi All,

Am struck with one of the requirement which I want to include in my script.

Have a script which call several sub functions, reads user inputs and perform actions.

requirement : Is to capture all the Inputs/actions/messages while executing script. (in short, messages displayed in cmd prompt while executing script)

I tried with create logfile & appending them using tee logfilename but couldn't succeed.

Anyhelp please...


T3RM1NVT0R 01-23-2012 12:12 PM

@ Reply
Hi VijayaRaghavanLakshman,

Welcome to LQ!!!

You can use script command to achieve what you are looking for. Once you type script command in the terminal it will create a file name typescript and start logging everything that ran in the terminal. Remember it will create the typescript file under the same directory where you were when you ran the script command.

Syntax is pretty simple:


to exit or end logging type the following:


and it will display a message saying "Script started, file is typescript"

You an put this command at the beginning of your script or you can run this on the terminal before executing your script. Just do man script to get more options on this command.

VijayaRaghavanLakshman 01-24-2012 02:55 AM

Thanks T3RM1NVT0R for the reply.

I did try using script command in both ways ie,

a. adding "script command" at the top of shell script :

outcome :

1. It does create file called "typescript" from where shell script executed but file size is zero.

2. As soon as i run the shell script i get the message & terminates the shell script.(though this script has interactive mode so that can get user input and perform actions based on selected choices)


vijay@vijay-laptop:~/shell_scripts$ ./
Script started, file is typescript

b. I tired another way as well, ie, typing script command first & then executing shell script.

Outcome :

1. Shell script run in interactive mode gets the options/choices and inputs from user and performs action but it doesn't store anything in "typescript" file (file size still zero)

Is there anything wrong in the way am using this script command. or is there any other way to achieve to log every actions/inputs performed during shell script execution.


catkin 01-24-2012 04:26 AM

Presuming you are using read to get user input and echo for output, how about replacing them with functions, say my_read and my_echo, which do what the builtins do but also write the input and output to a log file?

T3RM1NVT0R 01-24-2012 07:34 AM

@ Reply
You have to type exit command as I mentioned in my previous post (edited version) to end the script and wirte to typescript file.

Rember typescript file looks exactly the same how terminal looked at the time of execution of commands / script. So dont get confused.

VijayaRaghavanLakshman 01-24-2012 09:34 AM

Thanks for the reply

but it still didn't do any good.

T3RM1NVT0R - As you have pointed out i put both script & exit command but as soon as i run the shell script, it terminates the execution.



root@vijay-laptop:/home/vijay/shell_scripts# ./
Script started, file is typescript

it just create empty typescript file

VijayaRaghavanLakshman 01-24-2012 09:46 AM

At last, found out an alternative way to do this. ie, while running shell script we need to use this command :


root@vijay-laptop:/home/vijay/shell_scripts# script -c ./ Install.log
Once the shell script execution done, we can see

Script done, file is Install.log

Am in the log file, can see all the actions/user inputs/commands type...

If anyone knows better way then do please let us know.


catkin 01-25-2012 08:00 AM

I suggested what I believe is a better solution in this post.

All times are GMT -5. The time now is 06:15 PM.