Help with implementing logging


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with implementing logging
# 1  
Old 01-11-2010
Help with implementing logging

I'm trying to add logging to an existing script which echos a number of lines to the screen. I've added a switch to the script that is going to suppress much of this output and put it in a file instead.

The way I envisioned it was like this:

$log would be set to either "" or the log files redirection

Code:
log=">> ./tstlogfile.txt"

echo "Text to terminal"

echo "text to log file" $log

I thought this would work, but it takes my redirection string literally... is there an easy way of doing this that I'm missing?
# 2  
Old 01-11-2010
try
Code:
LOGFILE=`./tstlogfile.txt`
echo "text to terminal"
echo "text to logfile">> $LOGFILE

# 3  
Old 01-11-2010
Yeah, I thought of that, but it would error when logging wasn't on because logfile would be empty.
# 4  
Old 01-11-2010
Quote:
Originally Posted by cheetobandito
Yeah, I thought of that, but it would error when logging wasn't on because logfile would be empty.
Log file will not become empty, as this is not redirection (>), that is append operation (>>).
# 5  
Old 01-11-2010
This (slightly edited) code works on our HP-UX machine.

Code:
LOGFILE=$LOGDIR/tstlogfile.txt
echo "text to terminal"
echo "text to logfile" >> $LOGFILE

# 6  
Old 01-11-2010
I meant the variable $LOGFILE would be empty which would error when the line gets interpreted as:

Code:
echo "text to logfile" >>

You'd get an error:

Code:
syntax error: `newline or ;' unexpected

# 7  
Old 01-11-2010
If your script doesn't use terminal for other purposes than logging, you can use exec
See tuto here Using exec
See example 19.2

Last edited by frans; 01-11-2010 at 12:19 PM.. Reason: precision
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

2. Shell Programming and Scripting

Need help in implementing expect

Hello All, I am trying a shell script for automatically login to test servers and pulling the output of top command from all using expect. ----snippet of code --- #!/usr/bin/expect -f #!/bin/bash server1=10.251.222.51 server=("$server1") i=1 for exp_server in ${server}; do expect -c... (3 Replies)
Discussion started by: Renjesh
3 Replies

3. Shell Programming and Scripting

Need help in implementing logic

i have following input file... 00290002STDR000000000000000000000000000EOD END TRANSACTION ^@^@^@^@^@^@^@^@^@^@^@^@^ 00299998STDR070000000007000000000000000STANDING DEBITS ^@^@^@^@^@^@^@^@^@^@^@^@^... (1 Reply)
Discussion started by: sagarrd
1 Replies

4. Post Here to Contact Site Administrators and Moderators

Constant Logging In (After Logging Out)

Hi Everyone. First, I want to thank all of you for letting me participate in this great group. I am having a bit of a problem. After I get an email from a responder, I login to make my reply. In the mean time I get another response by email from another member, I go to reply to them and I... (6 Replies)
Discussion started by: Ccccc
6 Replies

5. Shell Programming and Scripting

Implementing Password

I am trying to implement a login screen to the following code how would i go about doing so. I have try to place the password in a variable using if statements which would usually work but as i have the system in a while loop i think i need to find another method. #!/bin/bash #Filename:... (4 Replies)
Discussion started by: warlock129
4 Replies

6. Programming

Implementing the TCP stack

Hello, I am trying to implement TCP protocol in C. I have read the RFC for TCP and have knowledge about it. But I am stuck in coding. Are ther any reference links, code snippets available for reference? (1 Reply)
Discussion started by: Harsh
1 Replies

7. IP Networking

implementing ftp

i have a client server connection steady and running... but the problem here is that the file transfer is very crude and succeptible to risks... so i want to implement ftp.. can anybody suggest a way to implement it or any book to read? (4 Replies)
Discussion started by: damn_bkb
4 Replies

8. Programming

Implementing the redirection

Hi all I am facing a problem with redirection. Its somewhat related to parsing. I am following the following steps. 1. take the command and tokenize it. 2. if redirection is there then give it to redirection unit 3. if pipe is there give it to piping unit. 4. do until the command ends ... (0 Replies)
Discussion started by: mobile01
0 Replies

9. Programming

Implementing a shell in C

Hi, I am implementing a shell in C, with the following problem... Suppose the shell is invoked from the command line as >> myshell < test.in > test.out 2>&1 I have to execute the commands in test.in and redirect them to test.out How does one detect in the main function that the shell... (1 Reply)
Discussion started by: jacques83
1 Replies

10. Programming

Implementing a shell

I'm implementing a shell in C that supports piping, output redirection, and background processing, and a few other commands. I was wondering how I'd go about implementing the output redirection. So, I'd open a file and I'd fork and execute the command. But how would I get stdout into the file? Any... (10 Replies)
Discussion started by: ununium
10 Replies
Login or Register to Ask a Question