Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to capture shell scripts ouput to a log file .

 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the "tee" program:

script | tee logfile

will send one copy of standard output to the console, and the other to "logfile".
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you are talking about this:
correct me if am wrong
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.[/]
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a project gnu-utils or win32 ports of unix-utils, which contains a tee and is FOSS (GPL).

http://unxutils.sourceforge.net/
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan.
I will look into it.


Regards,
-Ricky
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic