Visit Our UNIX and Linux User Community


Suggestions on constructing a log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Suggestions on constructing a log file
# 1  
Old 08-14-2009
Suggestions on constructing a log file

Hi,

I have a series of BASH shell scripts that run together. I would like it so that these scripts construct a log file as they run. This log file would ideally be a text file that contains exactly (including blank lines) what is output to the terminal.

What is the best way to accomplish this? My initial assumption is that I will use the "tee" command, ie:

Code:
echo 'Start Processing' tee -a 'log.txt'

However I wonder if this is the most efficient thing to do, as I will be using that command A LOT? Does anyone have any suggestions?

Thank you for the help,
Mike
# 2  
Old 08-14-2009
Try the 'script' command. It gives you an interactive shell that records the session into the file 'typescript'.

Or you could just redirect the output of the scripts themselves instead of trying to build in the redirection from the inside.
# 3  
Old 08-14-2009
Another way for simple cases is to enclose the script (or parts of it) in curly brackets and direct the output from that to a file:

Code:
TestScript.sh
===========
#!/bin/bash
 
{
function TestFunc {
  echo "$(date):  $0:  $@"
}

TestFunc this is
TestFunc just
TestFunc a test
} | tee MyLog.txt
 
 
cat MyLog.txt
Fri Aug 14 11:19:54 UTC 2009:  TestFunc:  this is
Fri Aug 14 11:19:54 UTC 2009:  TestFunc:  just
Fri Aug 14 11:19:54 UTC 2009:  TestFunc:  a test

You can apply the same on the command-line

Code:
{
  date
  sleep 1
  date
  sleep 1
  date
} | tee dates.txt
Fri Aug 14 11:27:46 UTC 2009
Fri Aug 14 11:27:47 UTC 2009
Fri Aug 14 11:27:48 UTC 2009

cat dates.txt
Fri Aug 14 11:27:46 UTC 2009
Fri Aug 14 11:27:47 UTC 2009
Fri Aug 14 11:27:48 UTC 2009


Last edited by Scott; 08-14-2009 at 03:50 PM..
# 4  
Old 08-15-2009
Plan B: 'exec'

Previous Thread | Next Thread
Test Your Knowledge in Computers #707
Difficulty: Medium
The Web console is a debugging tool in the browser which should only be opened and used by web development experts.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading and Writing a conf file - Suggestions and improvements?

Hello all As part of my TUI - (line based) Text User Interface, i do have 2 commands to assist working with conf files. Now, for me they work, but since i wrote them, i automaticly use them they way they should be used... you know what i mean. ;) Anyway, they are designed to read 'simple'... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

Constructing a Matrix

Hi, I do have couple of files in a folder. The names of each of the files have a pattern. ahet_005678.txt ahet_005898.txt ahet_007678.txt ahet_004778.txt ... ... ahet_002378.txt Each of the above files have the same pattern of data with 4 columns and have an header for the last 3... (4 Replies)
Discussion started by: Kanja
4 Replies

3. UNIX for Dummies Questions & Answers

constructing a file for printing

I have a text file, data.txt, which looks like :- head1 head2 head3 data1 1 data 1 2 data 1 3 data 2 1 data 2 2 data 2 3 data 3 1 data 3 2 data 3 3 etc etc I would like to print this file, using the lp -d command, with a heading line, as follows :- Report for whatever department as... (2 Replies)
Discussion started by: malts18
2 Replies

4. Shell Programming and Scripting

Constructing numbers from input lines

I have a file with the information shown and I want to capture the entry having the rgdt tag and taking the value (the location of the represents the decimal point, for example 0p50 represents 0.50) I then want to divide the number at the end of each line by the value 0.50 I want to do... (7 Replies)
Discussion started by: kristinu
7 Replies

5. Shell Programming and Scripting

Constructing a string by reading the contents of 2 files

Hi all - I am trying to write a script to generate a string that gets sent to an IP phone using wget. This is to upload a phone book of up to 100 entries so the string needs to be constructed in one 'hit'. I am a relative newbie to shell scripts but willing to try anything! I have 2 files: ... (3 Replies)
Discussion started by: cakerack
3 Replies

6. Shell Programming and Scripting

Suggestions for adding columns to text file

Good afternoon to everyone, I have some input and output from various widgets that I am trying to get to play nicely together. Basically I would like to stay out of excel and be able to automate the entire process. I have read some posts here about how to use awk, nawk, etc, to do similar... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

7. Shell Programming and Scripting

Help With Constructing A Korn Shell Search Loop

Hello All, I am a statistician and I am very new to the world of ksh programming. Daily, I analyze millions of rows of data and land information to DB2 tables. I have recently been asked to develop a ksh script to FTP an export file containing line item data from the production environment to the... (2 Replies)
Discussion started by: jonesdk5
2 Replies

8. Programming

Constructing a server daemon / questions on handling clients

Hi All, I'm interested in the best way to serve multiple clients from a server process. Now, our server is going to serve data on a custom protocol and it is going to get / process that data from a number of possible back-ends including files (text or xml) and MySQL/some other database.... (2 Replies)
Discussion started by: dragonfly
2 Replies

9. Shell Programming and Scripting

Suggestions for converting a file

I am trying to convert a list of information that has 6 or 7 lines in it, into a delimited file. The file looks like this: Company Contact Address City, St, Zip Phone email website (optional) <blank line b/t records> I would like the output to be: Company,Contact,Address,City, St,... (7 Replies)
Discussion started by: goldie363
7 Replies

Featured Tech Videos