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.
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.
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.
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).
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.
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.