0

I'm trying to redirect the output of my script and it needs to be called inside the script.

filename=uname -a
filename="$filename" date

2>&1 | tee $filename".txt"

That is what I have so far, but it's obviously wrong. I don't know too much SH scripting, so help is appreciated

-Alex

Buzkie
  • 859
  • 3
  • 11
  • 21
  • possible duplicate of [redirect COPY of stdout to log file from within bash script itself](http://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-to-log-file-from-within-bash-script-itself) -- sorry for the shameless advertising, but I didn't want to copy my answer over. ;-) – DevSolar Oct 30 '13 at 08:24

3 Answers3

5
filename=uname -a
filename="$filename" date

2>&1 | tee $filename".txt"

... If you look at the code I posted above I'm trying to dynamically name the log and use tee to print to both the console and the command line.

I think you're looking for something like this:

filename="$(uname -n)-$(date +%F).txt"
{
  dostuff
  domorestuff
} 2>&1 | tee "$filename"
dubiousjim
  • 4,722
  • 1
  • 36
  • 34
1

its wrong because you are not assigning your variables properly. Assuming date is GNU date, and not some "date" function you created. you might be looking for. Use $() syntax to execute commands and put to variable.

filename="$(uname -a)$(date)"

By the way, are you sure you want to use uname -a for filename ? you might also want to consider formatting your date like date +%Y%m%d-%H%m%s for example.

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

You should just print everything to console with echo. Then you can redirect that to a log. For example:

# my_file.sh
echo "Doing stuff"
# Do stuff

# Invocation:
my_file.sh > my_log.log

This gives you the most flexibility and elegance.

You can also write to log directly from script, like:

# my_file.sh
echo "Doing stuff" > my_log.log
# Do stuff
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
  • That doesn't really answer my question. If you look at the code I posted above I'm trying to dynamically name the log and use tee to print to both the console and the command line. – Buzkie Mar 08 '10 at 18:49