Two Laptop Bag*
The moose likes Linux / UNIX and the fly likes How to capture shell scripts ouput to a log file . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to capture shell scripts ouput to a log file ." Watch "How to capture shell scripts ouput to a log file ." New topic
Author

How to capture shell scripts ouput to a log file .

Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Hii, folks

I have a shell script, which thorws some log messages in standard output .
I want to catch that output from inside the same shell script to a log file.But here am facing a problem and the problem is :
I tried :

Problem:
It successfully captured the output but It stopped throwing the log messages in console. All the log messages got diverted to the stdout and stderr file which i dont need. I need the script should throw log as well as record it.
Like in script command it throws logs as normal process as well as records it.
Please help!
Thanks ricky


Regards, Ricky
Oracle Weblogic 10g Certified Expert TechPaste.Com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You can use the "tee" program:

script | tee logfile

will send one copy of standard output to the console, and the other to "logfile".


[Jess in Action][AskingGoodQuestions]
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

I want to invoke the catch command from the inside the same file.
i think in tee i need to start my install.sh script as
./install.sh | tee install.log
but i dont need this too
i want the to be invoked from inside only.
so that i will only run ./install.sh
and the command inside will capture all the out put .
am able to use "exec 1 > log " from inside my script.
but still as i said before i want log in both on console and in log file.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11524
    
100

Originally posted by Ricky Rosan:
i think in tee i need to start my install.sh script as
./install.sh | tee install.log


man tee

You will see it has an append option. So you can have it on every line within your shell script that you care about. You do not need it on the command line itself.

Alternatively, you could consider wrapping your code within a function:

That way you only have your tee (and standard output) redirects in one place. And as an added benefit your code could be more modular and easy to understand.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

so you are talking about this:
correct me if am wrong
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Hi All,

Dont know this is the right place to post this or not as its a windows thing.
I've one doubt is there way or command to catch the same outputs of a batch file run, to a logfile.
like echo blah blah > blah.log
I mean to say like in Unix we have tee do we have a replacement in windows also.
or do i need to create a function kind of thing to get a logfile from the batch file run.


Thanks,
Ricky
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

I think if you can use the cygwin system, you'll find it supports "tee" under Windows. There's also a utility of 2 that does what tee does that runs in native Windows command-shell, though I don't know any names. If all else fails, tee is just a simple program that reads a line and outputs it to t different files, then repeats until end of input.

If you're referring to Java logging, however, there's another possibility. The major Java loggers (apache commons, log4j and java.util.log) have the ability to be configured to log to more than one destination, and even to log selectively to a destination. For example, all SEVERE-level messages might be routed to a printed console and to a paging system such as Big Brother, all com.oracle-class messages of might go to a DBA's audit file, [i]etc.[/]


Customer surveys are for companies who didn't pay proper attention to begin with.
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Ya thanks,

but cygwin is not an option for me here. As i cant suggest a user to install cygwin before running my batch file.
But what i found is its not bad idea to pack the tee.exe file from cygwin with my batch file as the size is also 23.8kb.

Is it a good idea or not.??

Cheers!!
Ricky.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

You can try, but tee.exe may call on other cygwin components, so I'm not sure it would work.

But, like I said, you should be able to find a DOS-native version of tee with a little searching. The main difference is that tee is part of the basic package in *n*x but not Windows. So is grep, however and I've used grep since my CP/M days!

And if all else fails, you can write your own in under an hour.
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Yes Very true.

I will look into it and let you know.
Else its not a bad idea to write my own logger

It will not also take me much time.
Thanks for the idea.

-Ricky
-------
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

There is a project gnu-utils or win32 ports of unix-utils, which contains a tee and is FOSS (GPL).

http://unxutils.sourceforge.net/


http://home.arcor.de/hirnstrom/bewerbung
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Thanks Stefan.
I will look into it.


Regards,
-Ricky
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to capture shell scripts ouput to a log file .