command output to variable assignment and screen simultaneously


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting command output to variable assignment and screen simultaneously
# 1  
Old 09-28-2009
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  
Old 09-28-2009
Code:
output=$(time /usr/local/bin/rsync -via /path/to/sync/ rsync://host1/destination" 2>&1 | tee /dev/tty)

# 3  
Old 09-29-2009
Quote:
Originally Posted by jlliagre
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble with variable and command assignment

I have a section of a script where I want to check a log file for a certain oracle error and if there is only one error (and it is ORA-39152) then I want to email that script is complete. Otherwise email failure and log. Somehow with this while the log only has one error and it is ORA-39152, I... (5 Replies)
Discussion started by: cougartrace
5 Replies

2. UNIX for Dummies Questions & Answers

Send output of command to screen and to multiple files

Hi there, I'm a newbie to Unix (taking a course in it right now) and I don't know how to do the following in bash: I need to write a command to display information about the used and free space on the file system, showing only local file systems, and then send the output of the command to... (1 Reply)
Discussion started by: damianberry
1 Replies

3. Solaris

Looking to understand what this command $$ does in variable assignment?

Hi Folks, I'm looking to figure something out in an existing script I'm trying to understand. the command in question(on a Solaris Box using KSH) is: WORKDIR=/tmp/namereplaced.exec.$$.$RANDOM Now, I know it's setting the $workdir environmental variable... And I understand most of... (2 Replies)
Discussion started by: Marc G
2 Replies

4. Shell Programming and Scripting

Redirect an output from a script to a file and display it at a console simultaneously

Hi, I'd like to redirect the STDOUT output from my script to a file and simultaneously display it at a console. I've tried this command: myscript.sh | tail -f However, it doesn't end after the script finishes running I've also tried this: myscript.sh | tee ~/results.txt But it writes... (3 Replies)
Discussion started by: wenclu
3 Replies

5. Shell Programming and Scripting

Screen output is blocked by "| tee" command

BACK STORY: I have a script build.py . (It's for creating the ISO file for a special edition of Swift Linux.) This build.py script executes the mintConstructor.py script that I use to modify the Regular Swift Linux ISO to get the special edition Swift Linux ISO. The lines of the script that... (2 Replies)
Discussion started by: swiftlinux
2 Replies

6. Red Hat

command line tool to disable screen lock and/or screen saver

Hi, I have a simple question : how to disable screen lock and/or sreen saver with command line with RHEL5.4 ? (1 Reply)
Discussion started by: albator1932
1 Replies

7. Shell Programming and Scripting

zenity progress and simultaneously terminal output

Hi, I want to use zenity --progress and also put the output to the terminal. I tried using the tee command but that puts the output to the terminal first and then shows the zenity progress dialog. Take the normal example by the gnome manual: ( echo "10" ; sleep 1 ... (0 Replies)
Discussion started by: sikku
0 Replies

8. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. UNIX for Dummies Questions & Answers

Seeing the screen output beyond the scroll capability for the last run command

HI , I forgot to redirect my op to a file.The op which is quite huge , thus printed on the screen.However bcoz of the limited viewing in the screenI can not see the whole of the output.. Is there anyway I can see the full op.My run takes half a day for finnishing ..So I am refraining... (1 Reply)
Discussion started by: bimukt
1 Replies
Login or Register to Ask a Question