Redirecting output that was redirected to variable and console


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting output that was redirected to variable and console
# 1  
Redirecting output that was redirected to variable and console

Hi all,

I would like to store the output of a command in a variable and output it to the console at the same time. This is working fine using the following construct
Code:
var=`command | tee /dev/tty`

I use this in some scripts to display the output of the command on the console and, at the same time check the output for certain flags, which indicate successful/failed execution (This may not be good practise and would better be done using the return code of the command, but I have no control over the command and some errors can only be detected by checking for certain output).

The problem is that the ouput of the above command can no longer be redirected. As an example:
Code:
var=`echo test | tee /dev/tty` > /dev/null

will still output test.

Is there a better way to do this? Of course there are alternatives:
  • I could only store the output in the variable and not write it to the console simulteaniously. Once the command has completed the variable could be printed to the console. Though, I think its nice to get a live view of whats going on.
  • Also, I'm aware that I could just tee the output to the console and a file and subsequently grep for the flags in the file. Though, I'd like to avoid creating a file.
So, is it possible to redirect the ouput of a command to the console and to a variable in a way, that you could also redirect the output that goes to the console at a later point in time?

Thanks for your help
Ben
# 2  
A possible technique :
Code:
if display required
then
   Console=/dev/tty
else
   Console=/dev/null
fi

var=$(command | tee $Console)

Jean-Pierre.
# 3  
Thanks for the reply.

Please note that the output should be redirectable to a file. The example redirecting to /dev/null
Code:
var=`echo test | tee /dev/tty` > /dev/null

was only to illustrate the problem.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #574
Difficulty: Medium
The process of allocating and deallocating memory is generally referred to as memory swapping.
True or False?

10 More Discussions You Might Find Interesting

1. Linux

RHEL 6.3 - chage command not redirecting the output to console.

When i am issuing chage command, it reporting the output properly. But when i redirect the output, i am not getting the output in the mentioned path. chage -l root >> /tmp/chage.txt. I need to use this into the script to capture the data. I think its seems to be bug with RHEL 6.3. Same... (3 Replies)
Discussion started by: Srini.rk1983
3 Replies

2. Solaris

Redirecting console output to ILOM during installation

Hi, I need to install solaris remotely via the ILOM port on a x4170 box, and I have downloaded the Solaris 11 for x86 text install image and burned it on a DVD. When I boot the box, its showing the boot_archive loading percentage on the SP console, but when its done and start the interactive... (0 Replies)
Discussion started by: u_paludan
0 Replies

3. UNIX for Dummies Questions & Answers

Dot and redirected output syntax in a tar command

Hi All, Please could anyone advise what the purpose of the dot syntax in the following command means: tar -cvf ${WORKING_BACKUP_ROOT}/${TAR_ARCHIVE_FILE} . >/${BACKUP_ROOT}/${ARCHIVE_LOG} Many thanks (2 Replies)
Discussion started by: daveu7
2 Replies

4. UNIX and Linux Applications

missing delimiters when mysql output is redirected to log file

Hi, Pls check that '|' and '+' present in Step-1 are not copied to log file in Step-3. Pls suggest how to get the exact output from Step-1 (i.e. with out losing '|' and '+') in to a log file ~Thanks Step-1: Execute command > mysql -utest -ptest -htesthost testdb -e "select * from... (3 Replies)
Discussion started by: newbielgn
3 Replies

5. Shell Programming and Scripting

Redirecting output to both console and to a file

Hi All, Is there a way in Bash we can redirection some output to both console and the file at the same time. ~Parag (2 Replies)
Discussion started by: paragkalra
2 Replies

6. Shell Programming and Scripting

Exclude Certain Entries from Piped or Redirected Output?

I want to execute a command something like: find / -name "jni.h" and I want to direct the output of that command to some type of filter that will leave out all the lines reporting inaccessible directories (permission unavailable). Is this a pipe or a redirect? For example, output like... (1 Reply)
Discussion started by: downplay
1 Replies

7. Shell Programming and Scripting

problem redirecting output of command to variable

Hi. I'm a newbie in scripting and i have this problem: i want to use the 'fuser' command on a file to tell if it's being accessed (for my purposes: still being written). I want to save the output of the command and later compare with the 'not being used' result. the script: #!/bin/bash... (2 Replies)
Discussion started by: nunovc
2 Replies

8. UNIX for Dummies Questions & Answers

redirected output not going to file for all cases

I have to confirm that an engine was not able to run. In the output below you see that it indeed got errors, but it didn't send those messages to the output file. When I run the same thing with a different executable it works. So does this mean something in the executable could cause it not to... (7 Replies)
Discussion started by: brdholman
7 Replies

9. Shell Programming and Scripting

redirected output

Hello Everyone, I have an option for users in my shell script to create log file. So if user saying yes on it, Im redirecting all output to log file by doing this: > /output.log. However I would like the output being displayed on the screen at the same time. Is it possible? If yes, does anybody... (2 Replies)
Discussion started by: slavam
2 Replies

10. UNIX for Dummies Questions & Answers

Standard output not redirected from /bin/sh

I have an application which has a lot of cout & cerr statements. This application also opens a log file (for eg a.log). When this application is run from the inittab file as follows /bin/sh -c " . /etc/timezone; exec /test" all the cout & cerr statements are printed in the log file... (1 Reply)
Discussion started by: soorajmu
1 Replies

Featured Tech Videos