![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
Quote:
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. |
![]() |
| Bookmarks |
| Tags |
| output, screen, shell script, variable |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|