Register
It is currently Mon Sep 01, 2014 5:27 pm

Log to file or screen


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed May 04, 2011 5:15 am   

Joined: Wed May 04, 2011 5:07 am
Posts: 2
Hello, Im trying to build a small log-function for logging to file or the screen

---------------------------------------------------

# The directory I want to log to
LOGDIR="/var/log/"

# If LOGGING = 1 log goes to file
# If LOGGING = 0 log goes to screen

LOGGING="1"

if [ $LOGGING == "1" ]; then
LOG1="$LOGDIR/$USER.log"
echo "Logging to $LOG1 & $LOG2"
elif [ $LOGGING == "0" ]; then
LOG1=""
echo "Logging to screen is on"
fi

echo "log this text" >> $LOG1

---------------------------------------------------
If LOGGING="1" then all works and the output goes to the file, but if I change to LOGGING="0" it does not work any more, any good ideas on how to solve this, or is there maybe a better way to do it? For what I know, I think the problem is in the >> but I'm not sure how to get around that thing?


Top
 Profile  
 PostPosted: Wed May 04, 2011 9:37 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
You have several options.
For example, you can use a function:
Code:
function log () {
    if (( LOGGING )) ; then
        cat >> $LOG1
    else
        cat
    fi
}

Then just pipe whatever you want to log to the function:
Code:
echo This goes to the logfile | log


Set LOGGING to 1 and LOG1 to a filename to log to the file.

Or, you can set LOG1 to /dev/fd/1 (depends on your system) to output to screen or to a filename to ouput to the file.


Top
 Profile  
 PostPosted: Wed May 04, 2011 1:15 pm   

Joined: Wed May 04, 2011 5:07 am
Posts: 2
Thx, this solved my problem :) now it works great


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP