LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2005, 01:54 AM   #1
Furlinastis
Member
 
Registered: Dec 2004
Location: Ball of Confusion
Distribution: Artix,Arch,Slackware,Bluewhite64
Posts: 261

Rep: Reputation: 40
redirect stdout to a varible


I want to time stamp each output to a log file in a bash script like so:

#!/bin/bash
date [operator] variable
echo $variable': Something happened' >> log

Is there a way to redirect the output of date to a variable? Or is there a better way to accomplish this?

Last edited by Furlinastis; 12-06-2005 at 01:59 AM.
 
Old 12-06-2005, 04:12 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
With bash you can use $() syntax or inversed quotes for other shells
Code:
time=$(date <date options...>)
echo "$time: Something happened" >> log
 
Old 12-06-2005, 04:38 AM   #3
Furlinastis
Member
 
Registered: Dec 2004
Location: Ball of Confusion
Distribution: Artix,Arch,Slackware,Bluewhite64
Posts: 261

Original Poster
Rep: Reputation: 40
oh ok, thanks
 
Old 12-07-2005, 06:01 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I usually do something like this:

Code:
LOG_WITH_APP_AND_TIME()
{
   exec > $LOGFILE    # redirect to logfile?

    time=$(date +%H:%M )
    if [ $# -gt 0 ];then 
 /usr/bin/echo [${0##*/}-$time] $@
 else
 /usr/bin/echo [${0##*/}-$time]
 fi
  
}


alias LOG=LOG_WITH_APP_AND_TIME
if you want all output to a log file
call the script with the log file name scrip 2&1> >> logfile.

best not to sprinkle echo > file type things everywhere.
 
  


Reply



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
TCSH: redirect stdout and stderr seperately ugenn Linux - General 3 02-07-2016 03:23 PM
redirect AND print stdout samel_tvom Programming 12 10-07-2012 05:37 PM
how to redirect stdout [binary stream] to multiple processes vtaminh Linux - General 2 08-19-2004 01:05 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM
command output to varible!!! fooforon Programming 3 02-05-2004 08:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:02 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
Open Source Consulting | Domain Registration