Search

Command to save commands being run

Script :

The command script is very useful for students and faculty. It helps to save the commands and the output run on the terminal to a file, which can be submitted as an assignment by the students.


Working:

It can be started as follows.

$ script
Script started, file is typescript
$



The default file in which the commands are saved are "typescript", to create a output file by any other name use the -f option.

Any command that you run after this and its output will be saved in a file by the name typescript in the same folder where you ran the command script. 

For eg if we run the following commands after running the script command.

$ ls
temp test file1 typescript
$ cat temp
Hello
$


To stop the script hit "cntrl + D" ( for Bash shell)  or exit (for C shell).

Now look into the typescript file to see what it has saved

$ cat typescript
Script started on Wednesday 06 July 2011 04:58:13 PM IST
$ ls
temp test file1 typescript
$ cat temp
hello
$ exit
Script done on Wednesday 06 July 2011 05:05:32 PM IST


We see that the typescript command also gives the time of starting as well as ending of the script.

To save the output in a file in another file name start the script with the option "-f"

$ script -f output
Script started, file is output
$


As stated , the output will be saved in a file named "output".

You can also append the output to a previously existing file using the "-a" option.


Internally the command script forks another shell in which the commands are executed, and saved in the typescript file. When "cntrl+D" or exit is executed the sub shell gets killed and the command stops running.
In case just one command has to be executed and we do not want to launch another shell then we can use the option "-c"
$ script -c ls
This will put the command output into the typescript file and ends by itself.




So next time you want to save your terminal session you know what command to use. :)

No comments:

Post a Comment