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

» For Loops in PHP

To create a for loop in php, follow these simple steps:Set a counter variable to some initial value.Check to see if the conditional statement is true.Execute the code within the loop.Increment a counter at the end of each iteration through the loop.The following is pseudo php code for a for loop:123for(init counter; conditional statement; increment counter){ inner code;}As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to...

» Filter Spam and Messages Easily in Gmail

, Google's approach to email, has a little-known feature which makes it very easy to filter spam and easily manage email. Many people keep a "junk" email account which they use when signing up for websites and registration forms online. This way, when some of these websites spam their inbox with messages, it will go to their "junk" email account only and not their real one. Gmail has a way to eliminate the hassle of checking this separate account and wading through all of the useless emails in...

» Using Awk to Print Select Output

Awk is a very powerful utility, but its syntax is less than intuitive to a beginner. Here are a few common but very powerful things you can do with awk:1234567891011121314151617# print line 2 of the fileawk 'NR == 2' myFile.txt # print column 2 of the fileawk '{print $2}' myFile.txt # add up column 2awk '{s += $4} END {print s}' # print all the lines with more than 80 columnsawk 'length($0) > 80' myFile.txt # print the number of lines in the fileawk 'END { print NR }'...