3

I've got a bash script I'm working to improve, and have put on a great fix thanks to Dennis Williamson. Unfortunately, one of the lines no longer echoes into a variable I can manipulate, rather dumps the output directly. I'll be good to go if I fix this.

Why is this bash command not echoing into the $result variable and what can I do to improve?

  result=$( time wget -q --output-document=/tmp/wget.$$.html http://domain.tomonitor.com 2>&1; );

EDIT: Various solutions I've tried

  result=$( { time (/usr/local/bin/wget -q --output-document=/tmp/wget.$$.html --header="Host: blogs.forbes.com" http://$host) } &2>1 );

  result=$( { time (/usr/local/bin/wget -q --output-document=/tmp/wget.$$.html --header="Host: blogs.forbes.com" http://$host) } );

  result=$( ( time (/usr/local/bin/wget -q --output-document=/tmp/wget.$$.html --header="Host: blogs.forbes.com" http://$host) ) );

EDIT2:

I'm echoing out a line like this:

  echo "$date, $host, $result"

Date and host are currently fine. $result is not.

I'm getting lines like this:

3.887

Tue Feb 15 08:39:53 PST 2011, 192.168.0.2,

3.910

Tue Feb 15 08:39:57 PST 2011, 192.168.0.3,

I'm expecting lines like this:

Tue Feb 15 08:39:53 PST 2011, 192.168.0.2, 3.887

Tue Feb 15 08:39:57 PST 2011, 192.168.0.3, 3.910

editor
  • 383
  • 2
  • 5
  • 21
  • 1
    No, look carefully at the line in my answer (and the one in my answer to your other question). (The first line in your edit is truncated, by the way.) Please be specific regarding what you want to capture and where. – Dennis Williamson Feb 15 '11 at 16:47
  • Saw and fixed the truncated line. Checking on others. Thank you again for your time and patience. – editor Feb 15 '11 at 16:49
  • You are correct. I am not sure what I missed in your answer but it is certainly the right one. – editor Feb 15 '11 at 16:51
  • 1
    You corrected the truncated line, but I suspect that dollar sign at the end is an artifact of `nano`. – Dennis Williamson Feb 15 '11 at 17:08

2 Answers2

2

You need curly braces around the whole command inside the command substitution but before the redirection:

result=$( { time wget -q --output-document=/tmp/wget.$$.html http://domain.tomonitor.com; } 2>&1; );

That captures the output of time. If you want to capture the error output of wget (you're already saving its regular output to a file), you'll need to do something a little different and it depends on what exactly you want to do. It may involve extra file descriptors or simply using the redirection I showed in my answer to your other question (instead of using --output-document).

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Thanks (again!) Dennis. I started with that per your last note but somehow lost the curly brace while trying out different solutions. For me, it doesn't yet seem to work with or without. – editor Feb 15 '11 at 16:37
  • 1
    @editor: See my edited answer. Please let me know specifically what's happening - error messages, unexpected behavior, etc. Do you want the output of `time` in the variable? What do you want to do if `wget` outputs an error? – Dennis Williamson Feb 15 '11 at 16:40
  • 1
    It works for me! Which bash version are you using? Check it with "bash --version". Is $results empty? What do you expect to get there? The output of the time command? I do get it. – rems Feb 15 '11 at 16:42
  • Thanks for *all* the help, both Dennis and rems. I've tried to edit my answer to be more clear. The command itself is working for me, it just spits directly into the output buffer and not into a variable. The result is something that works, just not exactly as I intended. My bash version appears to be 3.2.48. – editor Feb 15 '11 at 16:45
  • 1
    @ editor: Please be *specific*. You say it "spits directly into the output buffer". It spits *what*? The output of `time`? The output of `wget`? The error output of `wget`? – Dennis Williamson Feb 15 '11 at 17:06
  • 1
    Try `var=$( { time sleep 1; } 2>&1 ); echo "[$var]"`. Is the output of `time` inside the square brackets or outside? – Dennis Williamson Feb 15 '11 at 17:11
1

I think this should go over to stack overflow but did you try this?

result=$( time (wget -q --output-document=/tmp/wget.$$.html http://domain.tomonitor.com 2>&1;) );
pablo
  • 3,040
  • 1
  • 19
  • 23
  • Just tried to no avail. I thought the command line-esque stuff came under the Serverfault umbrella but if someone needs to migrate I understand. – editor Feb 15 '11 at 16:33