Jump to content
xisto Community
jedipi

Running Shell Script And Redirect To Log File?

Recommended Posts

I have a shell script like this:while truedosh a.shsh b.shdonenow I want the script not just call and run a.sh and b.sh, but also output some log files.here is what I did:while truedosh a.shsh a.sh >>logsh b.shsh b.sh >>logdoneI know this is not a good way....is there any better way to do it??Thank you

Share this post


Link to post
Share on other sites

that IS a good way, thats te way you are meant to do it :)

you may want to tweak what shell output can, and cannot be logged.

sh a.sh | grep LOG >> log

this will only log lines with the word "LOG" in them.. for example
"LOG: 1:00pm, and all is well."

EDIT:
just noticed

while truedo
sh a.sh
sh a.sh >>log
sh b.sh
sh b.sh >>log
done


So, you want the script to print to the console, AND write to the log..
yes, there is a much better way, you dont need to run the script twice.

use a variable to store the output..

this code example run the porgram "uptime" and outputs its result to the scree, and a logfile.
the variable name is called CURRENT_UPTIME

#!/bin/bashCURRENT_UPTIME=`uptime`# print to screen.echo $CURRENT_UPTIME#print to logecho $CURRENT_UPTIME >> log

EDIT2:
it not clear on this forum, but those ` marks are the single back tilted quotes... using the normal ' will not work.

on my keyboard, the ` (correct mark) is the one below the ESC key.

in BASH shell programming, all 3 different quote marks have different meanings.

Share this post


Link to post
Share on other sites

Dear jedipi,

 

I coudn't help but notice in your example that you executed the shell commands two times:

sh a.shsh a.sh >>log

I suppose that the first execution was done to obtain the output on the console screen, and the second execution was done to obtain the output inside a file - what you call a log-file.

 

The danger here is that in some cases, it could be possible that the result would not be exactly the same - for instance when a time-dependent value was produced by the shell command.

 

There exists a shortcut to obtain the desired result by executing the bash script just once.

 

sh a.sh | tee log

 

The tee command is one of those odd little specialized Unix commands that come in handy at the strangest times. It both displays the command output on the screen, and stores it in a file. Use tee -a to append, instead of overwrite.

 

So, for the second bash script, we will use:

 

sh b.sh | tee -a log

 

 

I hope that I have interpreted your question correctly, and that my proposal will help you.

Share this post


Link to post
Share on other sites

lol, looks like we both posted at the same time, with different solutions.you should probably use signatureimage's solution, same results, minus the need for variables.been using bash for years, and thats the first time ive heard of that command (and yep, its installed on my system)

Share this post


Link to post
Share on other sites

lol, looks like we both posted at the same time, with different solutions.

 

you should probably use signatureimage's solution, same results, minus the need for variables.

 

been using bash for years, and thats the first time ive heard of that command (and yep, its installed on my system)

<{POST_SNAPBACK}>


thanks for you guys solutions.

 

With this tee command, I can split the output stream.

so that it is both displayed on screen and also re-direct it to a file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.