Redirect all output in shell script to a file

(0 votes, average 0 out of 5)

Redirecting a single command in a script to a file is straight-forward using the > syntax. For example, to redirect all STDERR output:

1
echo "hello, world" 2>> /tmp/filename.log

To redirect all lines of a Bash script to a file, add a scope around all of the lines and then redirect the scope to a file. For example:

1
2
3
4
5
6
(
echo "this is line 1 of my script"
cat /proc/cpuinfo
dmesg
echo "this is the end of my script"
) 2>> /tmp/filename.log

This allows you to capture all output of a script without having to append the redirection to each line.

Partner Links:
 
Related Articles

» Java - Reading and writing a properties file

A properties file in Java can be very useful when you start to notice that there are many locations in your code where you are either hard coding values, many constants, or using enums. Instead of including these values directly in your code, you can refactor them out to a properties file to separate responsibilities. Instead of potentially specifying values throughout many classes or programs, all values can be condensed into one or more properties files as need.An example of a properties file...

» How to Count the Number of Files in a Directory in Linux

Counting the number of files in a directory from a Linux command line is easy.  The following command will return the number of files in the current directory and all subdirectories: $> find directory_name -type f -follow | wc -l  This will count all files and files in subdirectories, but not the subdirectories themselves.  To find all the files in only the current directory, you can use: $> find directory_name -type f -maxdepth 1 -follow | wc -l  If you would like to...

» Copy or Backup Files Faster in Windows 7 or Vista Using Window's Robocopy

In all versions of Windows 7 there is a little known but easy to use utility bundled in called Robocopy.  Why would you want to use Robocopy over just dragging and dropping or copying and pasting files and folders over manually? Here are some reasons why you might want to add Robocopy to your toolbox of useful utilities:Robocopy is MUCH faster at copying a large number of files from one location to another compared to the built in Windows copy.Robocopy can intelligently "Backup" (Mirror) a...