LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-19-2010, 06:07 AM   #1
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 76

Rep: Reputation: 16
Enable/Disable logging in a Bash script


Hi all,

I have honestly searched around for an answer to this. I'm not sure if I am just banging the wrong keywords into Google but I am struggling to find the answer.

My objective is pretty simple: To have a variable within a bash script where I can switch between logging to file or screen.

Simple example of what I am trying to do:

Code:
vLOGPATH='/tmp/log.log'
vLOG='Y'

if [ ${vLOG} = 'Y' ]
then
        cat /dev/null > ${vLOGPATH}
        vLOGPATH='>> '${vLOGPATH}
else
        vLOGPATH=''
fi

echo "vLOG variable set to: "${vLOG}"."
echo "The log path ="${vLOGPATH}"."

echo "Test data line" ${vLOGPATH}
So, as you can see, I have a variable which clears the log path if the user variable vLOG is not 'Y'. This should mean the user can run the script from the command line and view all the output (instead of it going to a log file).

The problem is that with the final line of the script (which ultimately tests the intention), I don't get the required result as the vLOGPATH variable is just passed to echo as text.

I have tried using 'echo -e' and getting some backslashes in there, I have tried using eval but I am either using them incorrectly. I have tried different combos of using ', " or `, but I'm blatently too much of a dufus to get it right!

Could anyone help me out please?
 
Old 10-19-2010, 06:21 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
I have played with redirection a bit myself lately but I am also a little confused as to what you expect?

Assuming everything being echoed is to either go to the log file or to the screen, see if this is what you are after:
Code:
vLOGPATH='/tmp/log.log'
vLOG='Y'

if [[ $vLOG == Y ]]
then
    : > $vLOGPATH
    exec > $vLOGPATH
else
    unset vLOGPATH
fi

echo "vLOG variable set to: ${vLOG}."
echo "The log path = ${vLOGPATH}."

echo "Test data line"
Let me know if you have any questions?
 
1 members found this post helpful.
Old 10-19-2010, 06:44 AM   #3
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 76

Original Poster
Rep: Reputation: 16
Hi grail,

I have tested your suggestion and it seems to work great! Thanks for your reply

Basically I have some large crontabbed scripts which run various backups. They log rsync, date, echo, results to /tmp/log.log.

Example:

Code:
vDATESTAMP='date +%d"-"%m"-"%Y" - "%H":"%M":"%S" :"'
vLOGPATH=/tmp/log.log

echo `eval "${vDATESTAMP}"` "starting blah..." >> ${vLOGPATH}
rsync blahblah >> ${vLOGPATH}
echo `eval "${vDATESTAMP}"` "Ended blah" >> ${vLOGPATH}
echo `eval "${vDATESTAMP}"` "starting blah..." >> ${vLOGPATH}
rsync blahblah >> ${vLOGPATH}
echo `eval "${vDATESTAMP}"` "Ended blah" >> ${vLOGPATH}
Sometimes, when testing I want to run the script manually, so wish to see the log results on screen. Up until now I have been editing the filling to hash out the output redirects. This was getting tedious so I wanted a quick way of switching between logging to file or screen.

Would you mind explaining these two lines please?
Code:
    : > $vLOGPATH
    exec > $vLOGPATH
I am a little confused because I am used to using >> to append to logs, but your single > appears to still append? and is ':' another term for /dev/null?

Thanks in advance

Last edited by elliot01; 10-19-2010 at 06:45 AM.
 
Old 10-19-2010, 07:10 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
I thought I would mention that I have recently learnt that you should use the command 'eval' sparingly as it can have some very unexpected side effects.
In your example there is absolutely no reason to be using it at all.
Change the following:
Code:
echo `eval "${vDATESTAMP}"` "starting blah..." >> ${vLOGPATH}

#to
echo "${vDATESTAMP} starting blah..." >> ${vLOGPATH}
And you can remove the redirection obviously if using my previous example.
Code:
: > $vLOGPATH
This specific use of a colon ( is explained as:
Quote:
In combination with the > redirection operator, truncates a file to zero length, without changing its permissions. If the file did not previously exist, creates it.
You can read all the other things it can do here
Code:
exec > $vLOGPATH
This opens the file for all things sent to standard output to now go to the file. So instead of appending you can think of it as always open.

Note: Standard output, as it does not currently redirect standard error so errors will still go to the screen.

You can read more on using exec to redirect here
 
Old 10-19-2010, 07:39 AM   #5
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 76

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
In your example there is absolutely no reason to be using it at all.
Change the following:
Code:
echo `eval "${vDATESTAMP}"` "starting blah..." >> ${vLOGPATH}

#to
echo "${vDATESTAMP} starting blah..." >> ${vLOGPATH}
Wouldn't this mean the date would be the same time every time it's stamped?

I have a feeling this is why I was using eval, so it evaluated the date/time at the exact time it stamped it.

Just absorbing the rest of your reply
 
Old 10-19-2010, 07:48 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by grail View Post
This specific use of a colon ( is explained as:
Quote:
In combination with the > redirection operator, truncates a file to zero length, without changing its permissions. If the file did not previously exist, creates it.
That is correct but not the simplest way of thinking about it. Simpler is to say that <some command> > <file_name> sends the standard output of <some command> to <file_name>. ":" is the null command so it has no output so nothing is sent to <file_name>.

The actual sequence of events is: on finding the ">" operator the shell either empties <file_name> (if it exists) or creates it (if it does not exist) then it runs <some command>, directing any standard output to <file_name>.
 
1 members found this post helpful.
Old 10-19-2010, 07:53 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Here's a bash scrippet for logging/not logging according to whether the script is being run from a terminal or not
Code:
# Utility variables
# ~~~~~~~~~~~~~~~~~
readonly false=
readonly my_nam=${0##*/}
readonly true=true

# Set up output redirection and logging
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Assume nothing (except ps command) and provide for running both 
# interactively and non-interactively.
interactive=$false
# The "standard" test is to check $PS1 but this test is more reliable
case "$( /bin/ps -p $$ -o tty 2>&1 )" in
    *TT*-* | *TT*\?* ) 
        ;;
    *TT* )
        interactive=$true
esac
if [[ ! $interactive ]]; then
	redirect_fn=/tmp/$my_nam.$$.out
	exec 1>>"$redirect_fn"
	exec 2>>"$redirect_fn"
else
	exec 1>/dev/tty
	exec 2>/dev/tty
fi
 
1 members found this post helpful.
Old 10-19-2010, 08:01 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Quote:
Wouldn't this mean the date would be the same time every time it's stamped?
Yes sorry my bad there I read the following incorrectly:
Code:
vDATESTAMP='date +%d"-"%m"-"%Y" - "%H":"%M":"%S" :"'
I read the quotes as backticks
On that it is often clearer to use the $() compound in place of ``

I will also point out though that I would probably still remove the eval and place your date setup in a function:
Code:
log_date()
{
    date +%d"-"%m"-"%Y" - "%H":"%M":"%S" :"
}
And then call this in place of your variable:
Code:
echo "$(log_date) starting blah..." >> ${vLOGPATH}
 
1 members found this post helpful.
Old 10-19-2010, 09:04 AM   #9
elliot01
Member
 
Registered: Jun 2009
Location: UK
Distribution: CentOS / RedHat
Posts: 76

Original Poster
Rep: Reputation: 16
Well sincere thanks guys for all your help

Combining all of your suggestions and guidance my code is going to plan!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
enable OpenOffice macros from bash script debiprasad Linux - Software 2 06-10-2009 01:20 PM
Bash script. Rsync and MSSQL errors and logging. nattflyger Programming 4 07-19-2006 08:57 AM
Making a shell script to enable/disable apache/mysql Lazy Foo' Programming 2 01-27-2006 09:07 AM
Logging via bash script kleptophobiac Programming 8 07-22-2004 09:21 AM
disable logging rsbecker007 Linux From Scratch 2 04-09-2002 09:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration