Browsing all articles tagged with script

How to Copy Terminal Session into a File

Have you ever wondered how to copy the output of your terminal into a text file? Or maybe you teach Linux and you want to see what your students typed in and as well as the output? You think that running history is not enough? Then you need the script command.

Running script command

Open the man page of script command and you will see this:

Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

In a nutshell, it is history and tee all rolled into one. It will record everything you see on your screen, even the color. So if you typed in an invalid command, you will see the error in the log or if you run it correctly, you will have the output. But commands like top that refreshes the screen at an interval will most likely ruin the session or the log, so try to avoid similar commands.

To use it, just type the command script and it will begin recording the session. Once you are done, just type exit.

This is script in action:

rai@host1:~> script -a /tmp/script_test.log

Script started, file is /tmp/script_test.log
rai@host1:~> ls /home
R20 r200 R21 rai xx19
rai@host:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit
Script done, file is /tmp/script_test.log
rai@host1:~> cat /tmp/script_test.log
Script started on Mon 17 Jan 2011 06:24:12 PM PHT
rai@lhost1:~> ls /home
R20 r200 R21 rai xx19
rai@host1:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit

Script done on Mon 17 Jan 2011 06:24:54 PM PHT

The example above shows that script was started with -a option meaning it will append the output the specified file.

A better way to do this is to use it together with mkfifo command:

On Terminal 1 (Student’s terminal):

rai@host1:~> mkfifo /tmp/script_test.fifo
rai@host1:~> script -f /tmp/script_test.fifo

On Terminal 2 (Teacher’s terminal, same machine):

rai@host1:/tmp> cat /tmp/script_test.fifo

The above scenario will perform the following:
1) On the Student’s terminal, it will create an named pipe /tmp/script_test.fifo (man mkfifo) then run the script command with the -f option that ‘flushes’ out the output after each run. The Student’s terminal will look like it is not responding at this point, but don’t worry, it is perfectly normal.
2) On the Teacher’s terminal, the command cat will read the output file. Once you run the cat command, the session will be started.

Try the above steps and see how each screen behaves. Check also if doing the script command will create a populated output file.


Using Perl to Replace Text in Multiple Files

You can search and replace text inside multiple files using Perl. This is how you do it:

$ perl -i -pe 's/Windows/Linux/;' test*

This command will search for the word ‘Windows’ inside all files that begin with ‘test’. When it finds one, it will substitute it with the word ‘Linux’. This is useful if you have multiple files that contain the same text. Remember that you can use regular expressions to make searching flexible.


Tips: SSL Certificate Checker

If you have one too many SSL certificates to manage, there is one nifty program made with Bash that can determine a certificate’s validity.

SSL Certificate Expiration Check can do that for you and send the results to email. The program’s “send to email” feature allows the user to simply add it to cron to check the certificates and send an alert to the administrator if any certificate (from file or website) falls below the set expiration date.

The source can be downloaded here.


Tip: Detect Network Connectivity in Bash

There are times that we are required to check hundreds of servers for network connectivity, and what command comes first in mind when testing network – ping. To help out other system and network administrators out there, here is a quick bash script to ping servers.



for i in $(cat $SERVERS)
ping -c2 $i > /dev/null

if [ $? -ne 0 ]
echo "$i is DOWN"
echo "$i is UP"
sleep 3



Tip: How to Place Pause in Bash Scripts

Author Rai    Category Linux     Tags , , , , ,

There some scripts that need human interaction from time to time from confirming certain actions or to warn users that they are about to do something stupid ( “ rm -rf / “, perhaps?). In any case, putting a pause break in a Bash script is a very nifty trick to learn.

This is an example of how to use the Bash built-in command read to put pauses in scripts:


find /home -type f -name "*.txt"
read -p "Press [enter] key to delete *.txt files in /home or CTRL+C to exit"
find /home -type f -name "*.txt" | xargs rm -v

This example script will run the find command and display the results. If user presses the Enter key, the script will remove all files that were found, while pressing CTRL+C will terminate the script.

Here is another variation of the the script:


find /home -type f -name "*.txt"
echo "Press [enter] key to delete *.txt files in /home or CTRL+C to exit"
read contscr
find /home -type f -name "*.txt" | xargs rm -v

Powered by 1and1.comDomain Registrations starting at $9.98* Earn with Your BlogAdvertise @ PinoyTux

Search PinoyTux

Subscribe to Email Feeds

Enter Email Address:

Blog Lounge

Popular Posts

Recent Posts

Recent Comments

Site Stats