How to use tee and script commands to capture output and errors to file
How to use tee and script commands to capture output and errors to file
Spread the love

Need help troubleshooting your bash scripts? Give tee and script a go.

What happens when you’ve written a script to handle a crucial task on your Linux servers and you need the output of that script so you can either log what happens or troubleshoot what may not have happened during its run? If the script uses particular services, you could always view those particular log files, but that’s a bit cumbersome. And should the script fail, those service logs might not be any help.

Your best bet is using the always handy tee or script commands. The tee command reads from standard output and writes to standard output or to files, whereas the script command makes a typescript of a terminal session and can output to file. For anyone who does a lot of bash scripting, these commands should be considered must-have tools. This is especially true when your script outputs a good amount of information. 

I want to introduce you to the tee and script commands, so you can more efficiently troubleshoot and log your bash scripts.

SEE: 10 free alternatives to Microsoft Word and Excel (TechRepublic download)

What you’ll need

The only thing you’ll need to use either tee or script is a Linux server (or desktop). Both tee and script should be installed by default, so there’s no need to install a single package.

How to use the tee command

For the sake of simplicity, let’s write the always delightful “Hello World” bash script. That script will consist only of the following:

#!/bin/bash          
echo Hello World

Name that file test and give it executable permissions with the command:

chmod u+x test

If you execute that script with the command ./test, you should see similar output to that shown in Figure A.

Figure A

A working Hello World script.

” data-credit rel=”noopener noreferrer nofollow”>teea.jpg

A working Hello World script.

You may be asking yourself this one question, “Why don’t I just send the output of the script directly to a file?” You can do that with the command:

./test > test.log

You will see no output returned to the terminal as everything is dumped into the file test.log. To both view the output in real time, and have a log file of the output, you use the tee command like so:

./test | tee test.log

Not only will you see Hello World printed to standard output (the terminal—Figure B), you’ll also have the test.log file containing the same output.

Figure B

The tee command in action.

” data-credit rel=”noopener noreferrer nofollow”>teeb.jpg

The tee command in action.

How to use the script command

The next tool is called script. With script you get an interactive session, all of which is both printed to standard output and written to a file. To use script with our Hello World example, the command would be:

script -c ./test test.log

The -c option tells script we’re running a command. The output of the script command is shown in Figure C.

Figure C

The script output.

” data-credit rel=”noopener noreferrer nofollow”>teec.jpg

The script output.

The difference between the output to the file with the tee and script commands is that the script output will give you a bit more information, such as exit codes (Figure D).

Figure D

Exit codes are included in the script output.

” data-credit rel=”noopener noreferrer nofollow”>teed.jpg

Exit codes are included in the script output.

Naturally, when you have a more complicated bash script, you’ll see more telling output from the command. For example, let’s say you write a bash script that requires input of a filename and said file will be deleted. That bash script could look like this:

#!/bin/bash
echo "Enter filename to remove"
read fn
rm -i $fn

When you run that file with the script command:

script -c ./test test.log

You’ll be asked for the filename to be deleted (Figure E). Once the file is deleted, you can then view the test.log file to see the entirety of the session.

Figure E

A slightly more complicated bash file in the running.

” data-credit rel=”noopener noreferrer nofollow”>teee.jpg

A slightly more complicated bash file in the running.

When you have a very complex bash script, you’ll see quite a bit more (and useful) output written to the log file. 

And there you have it, two ways to capture the output of bash scripts both to standard output and to file. Enjoy this new level of bash script troubleshooting and logging.

Also see

linuxadminhero.jpg

Let’s block ads! (Why?)

Facebook Comments

Leave a Reply

Close Menu
%d bloggers like this: