
.
script - Creates typescript or saves terminal session
FORMAT:
script [-a] [-c COMMAND] [-f] [-q] [-t] [file]
DESCRIPTION
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hard-copy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.
Options:
-a Append the output to file specified or typescript, retaining the prior contents.
-c COMMAND Run the COMMAND rather than an interactive shell. This makes it easy for a script to capture the output of a program that behaves differently when its stdout is not a tty.
-f Flush output after each write. This will each time create a new file overwriting previous contents
-t Output timing data to standard error. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can be used to replay typescripts with realistic typing and output delays.
Note:- Certain interactive commands, such as vi(1), create garbage in the typescript file. Script works best with commands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal.
The environment variable SHELL is utilized by script:
Live Demos:
[email protected]:/home/shanky:> script
Script started, file is typescript
[email protected]:~> datre
bash: datre: command not found
[email protected]:~> date
Thu Jun 5 05:56:42 CEST 2014
[email protected]:~> cal
June 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
[email protected]:~> ls
04:06:14-04:26_shell.log dead.letter myscript.log somesed.sed testarg.ksh whileLoop.ksh
04:06:14-04:34_shell.log dfg perlTest somon_output test.sh
04:06:14-04:36_shell.log Documents public_html test tmp
abc.txt file_04-06-14-04:23_log qquniq.profile test1.sh typescript
bin mylog.log shanky.sh test2.sh untilLoop.ksh
[email protected]:~> exit
exit
Script done, file is typescript
Now we can see the log file of the session here. It will be containig the same thing what we did:
[email protected]:/home/shanky:> cat typescript
Script started on Thu 05 Jun 2014 05:56:35 AM CEST
[email protected]:~> datre
bash: datre: command not found
[email protected]:~> date
Thu Jun 5 05:56:42 CEST 2014
[email protected]:~> cal
June 2014
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
[email protected]:~> ls
04:06:14-04:26_shell.log dead.letter myscript.log somesed.sed testarg.ksh whileLoop.ksh
04:06:14-04:34_shell.log dfg perlTest somon_output test.sh
04:06:14-04:36_shell.log Documents public_html test tmp
abc.txt file_04-06-14-04:23_log qquniq.profile test1.sh typescript
bin mylog.log shanky.sh test2.sh untilLoop.ksh
[email protected]:~> exit
exit
Script done on Thu 05 Jun 2014 05:56:58 AM CEST
We can give a filename as per our choice to the log file:
[email protected]:/home/shanky:> script -f myscript.log
Script started, file is myscript.log
[email protected]:~> ls
04:06:14-04:26_shell.log bin file_04-06-14-04:23_log qquniq.profile test test.sh
04:06:14-04:34_shell.log dead.letter myscript.log shanky.sh test1.sh tmp
04:06:14-04:36_shell.log dfg perlTest somesed.sed test2.sh untilLoop.ksh
abc.txt Documents public_html somon_output testarg.ksh whileLoop.ksh
[email protected]:~> date
Thu Jun 5 06:00:02 CEST 2014
[email protected]:~> file shanky.sh
shanky.sh: ASCII text
[email protected]:~> exit
Script done, file is myscript.log
And here is the content of log file:
[email protected]:/home/shanky:> cat myscript.log
Script started on Thu 05 Jun 2014 05:59:47 AM CEST
[email protected]:~> ls
04:06:14-04:26_shell.log bin file_04-06-14-04:23_log qquniq.profile test test.sh
04:06:14-04:34_shell.log dead.letter myscript.log shanky.sh test1.sh tmp
04:06:14-04:36_shell.log dfg perlTest somesed.sed test2.sh untilLoop.ksh
abc.txt Documents public_html somon_output testarg.ksh whileLoop.ksh
[email protected]:~> date
Thu Jun 5 06:00:02 CEST 2014
[email protected]:~> file shanky.sh
shanky.sh: ASCII text
[email protected]:~> exit
Script done on Thu 05 Jun 2014 06:00:28 AM CEST
While defining the name of session log file using "script", we can use the time stamp for our ease.
Example:
[email protected]:/home/shanky:> script -f session_log_$(date +'%d:%m:%y-%H:%M').log
Script started, file is session_log_05:06:14-06:12.log
[email protected]:~> exit
exit
Script done, file is session_log_05:06:14-06:12.log
Each time you want to save your session you have to start the saving session using script command. But if you want to start
saving your session automatically when you open your session, you can achieve this by adding below command into your .bashrc
file present in home directory.
test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f ~/$(date +'%d:%m:%y-%H:%M')_shell.log);
The above command will start the script automatically when you start your Linux session and when you exit you can see the saved log.
This is very useful when a user wants to save his session record automatically when logs in and out the session.
If you want to save sessions of all users on that server, you have to make changes in /etc/profile file. Add the same line at the end
of this file.
If you like it, share it !!!
|