The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
variable assignment using awk sdosanjh Shell Programming and Scripting 8 07-29-2009 04:23 PM
Command display output on console and simultaneously save the command and its output satimis UNIX for Dummies Questions & Answers 7 01-25-2009 08:27 PM
Seeing the screen output beyond the scroll capability for the last run command bimukt UNIX for Dummies Questions & Answers 1 05-02-2008 05:22 AM
Variable assignment question DDD Shell Programming and Scripting 2 09-22-2006 03:32 PM
@ in a variable assignment rkap UNIX for Dummies Questions & Answers 1 05-23-2005 11:53 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2009
mbm mbm is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
command output to variable assignment and screen simultaneously

Hello Folks,

I have a script that runs a command (rsync) that sometimes takes a long time to complete and produces diagnostic output on stdout as it runs.

I am currently capturing this output in a variable and using it further in the script. I would like to continue to capture this output in a variable, but also display the output to the screen as it is produced from the command.

I am looking for a way to do this without creating a file on the filesystem. I am using Posix Shell on HP/UX and BASH on Linux (but open to other possibilities if necessary).

Currently I am doing this:

Code:
...
output=$(time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination" 2>&1)
echo "${output}"
...

Any suggestions will be appreciated,

-mbm
  #2 (permalink)  
Old 09-28-2009
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,478

Code:
output=$(time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination" 2>&1 | tee /dev/tty)

Bits Awarded / Charged to jlliagre for this Post
Date User Comment Amount
09-29-2009 mbm Simple and effective response. 1,000
  #3 (permalink)  
Old 09-29-2009
mbm mbm is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
Quote:
Originally Posted by jlliagre View Post
Code:
output=$(time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination" 2>&1 | tee /dev/tty)
Thanks jlliagre,

Man, talk about the forest hiding the trees! I tested this out, and it works great. I did have two hurdles. I'll explain them as others might have the same issues.

1) Wanted tty to be specified dynamically, and I tried originally with...

Code:
output=$(... | tee $(tty))

...but it wouldn't eval as the subshell wasn't attached to the tty. The obvious workaround was to set the tty to a variable first, then call it in the tee command:

Code:
tty=$(tty)
output=$(... | tee ${tee})

2) Since piping to tee yields a return code of 0 even if the rsync command fails, I have another isssue. Since I'm using Posix Shell and not BASH in HPUX, I don't have access to $PIPESTATUS.

Instead, I gathered the idea to prefix the rsync command with a trap to let me know if it failed like so:

Code:
output=$((trap 'RSYNC ERROR: $?' ERR && time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination) 2>&1 | tee ${tty})

Later on in the code, I can echo "${output}" | grep 'RSYNC ERROR' and parse out the return code if necessary.
Reply

Bookmarks

Tags
output, screen, shell script, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0