tee vs output redirection confusion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tee vs output redirection confusion
# 8  
Old 03-22-2009
Quote:
Originally Posted by c_d
ok i need help understanding the following points in the command by radoulov
1.why was rm f1 issued and then && applied?
You need to remove the f1 file to protect its content from the following cat/sort pipeline redirection.
The shell will read a block of data, process the lines in that data, and the converted lines will be written as a block. When you remove the file its content remains available on the disk until the file is closed and you can happily attach the cat/sort pipeline standard output to the new file f1.
Consider the following:

Code:
% printf '%-100000s\n' f1_line{1..3}>f1
% { rm f1 && cat - |sort > f1;}< f1    
% wc -l f1                         
3 f1
% { cat - |sort > f1;}< f1 
% wc -l f1                
1 f1

Quote:
2.which redirection has precedence over the other? < one first , or > one first? according to me output redirection(>) should run first because, its inside the { } ... but i dont think i am right...
A subshell is started with its standard input connected to f1.
The subshell runs the rm command (it happens to have its stdin connected
to f1, but this doesn't matter as rm ignores the input).
A new f1 is created and attached to the standard output of the pipeline, so then the pipeline has the
old f1 on its standard input, and the new one on its standard output. Notice that this:

Code:
% cat f1 && sort f1 > f1  
a
b
c

... is different than this:

Code:
% cat f1|sort > f1   
% wc -l f1
0 f1

Quote:
[...]
I am not really sure of that though...because replacing && with ; also works.
Yes, the ; works, but in some circumstances you can get an inconsistent result. Consider the following:

Code:
% head f[12]
==> f1 <==
this is file1
the quick brown fox jumped over the lazy dog
this is file1
who let the dogs out
this is unix
this is file1

==> f2 <==
this is file2
% chmod u-w .
% { rm f1 && cat - f2 | sort -u > f1;} <f1
rm: cannot remove `f1': Permission denied
% wc -l f1
6 f1
% { rm f1; cat - f2 | sort -u > f1;} <f1
rm: cannot remove `f1': Permission denied
% wc -l f1                              
1 f1

Hope this helps.

Last edited by radoulov; 03-22-2009 at 09:03 AM.. Reason: example adjusted
# 9  
Old 03-22-2009
Quote:
Originally Posted by c_d
[...]
but what seems like art to you, is like cipher text to me...Smilie [...]
May be this is better ? Smilie

Code:
cat f1 f2|sort -u 1<>f1

But it will fail in too many situations, so forget about it.
# 10  
Old 03-25-2009
thanks...i understand now Smilie
# 11  
Old 04-02-2009
hi

i was think of this again and i thought of this one

Code:
[c_d@localhost pipesnfilters]$ cat f1 f2 | sort -u 1>f1
[c_d@localhost pipesnfilters]$ cat f1

the quick brown fox jumped over the lazy dog
this is file1
this is file2
this is unix
who let the dogs out
[c_d@localhost pipesnfilters]$

any advise on this ? any cases where it could fail?

EDIT: now i see that in your above post you gave the same answer...why would it fail ? and in what cases?
# 12  
Old 04-02-2009
The command above works like this:

1. Begin executing the cat command reading a chunk of data from its first argument or STDIN (a limited number of bytes that varies from system to system) and passing that data through the pipe to the sort command.
2. Begin executing the sort command: first set up the redirection (f1 gets truncated) then wait for the entire input to come because of the nature of the ordering.

These too commands run in parallel.
Once the cat command has read and passed the first chunk of data, it will try to read the next one and in the case of f1 it won't be available anymore because of the following redirect.

Consider the following:

Code:
% printf 'f1%1000s\n' {1..1000} > f1    
% printf 'f2%1000s\n' {1..1000} > f2
% wc f[12]
   1000    2000 1003000 f1
   1000    2000 1003000 f2
   2000    4000 2006000 total
% cat f[12] | sort -u | wc -l
2000
% cat f[12] | sort -u > f1   
% wc -l f1
1000 f1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

output redirection

Hi all I was wondering if there was a slicker way of doing this without the file - awk '{print $2}' FS=":" "${FILE}" > "${TMPFILE}" { read M_GRP_ID || m_fail 1 "Error: Read failed 1 (${FUNCNAME})" read M_GRP_WAIT || m_fail 1 "Error: Read failed 2 (${FUNCNAME})" }... (6 Replies)
Discussion started by: steadyonabix
6 Replies

2. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

3. UNIX for Dummies Questions & Answers

Output redirection

Hello i am trying to write a script that will redirect the output to a certain file. Here is the code so far: #!/bin/bash ps -e | sort | more > psfile When I execute the script nothing happens since i assume the output was redirected to the file called psfile. When I try to look at the... (1 Reply)
Discussion started by: mfruiz34
1 Replies

4. Shell Programming and Scripting

Output redirection

We have an application here that does some table queries and then prints the result on screen. I do not have the code of this application (which i will just call "queryCommand"), but what it does is that you call it with some parameters and it prints some info about the query and then the... (5 Replies)
Discussion started by: jolateh
5 Replies

5. Shell Programming and Scripting

problem with suppressed output to file using echo and tee command

Hi, When I run the following command in terminal it works. The string TEST is appended to a file silently. echo TEST | tee -a file.txt &>/dev/null However, when I paste this same line to a file, say shell1.sh, and use bourne shell . I run this file in terminal, ./shell1.sh. However I... (1 Reply)
Discussion started by: shahanali
1 Replies

6. Shell Programming and Scripting

Redirection output

Hi there I have a script that runs but it outputs everything onto the screen instead of a file. I've tried using the > outputfile.txt however all it does is dump the output to the screen and creates an outputfile.txt but doesn't put anything in that file. Any help would be appreciated ... (6 Replies)
Discussion started by: kma07
6 Replies

7. Shell Programming and Scripting

redirection and output

I'm redirecting the output of a command to a logfile, however, if the user is on a terminal I would also like the output to be displayed on the screen. tar tvf some_tarfile >Logfile if the user is on a term then have the output to the Logfile and also be displayed on the screen at the same... (2 Replies)
Discussion started by: nck
2 Replies

8. Shell Programming and Scripting

How Unix tee to send pipeline output to 2 pipes ?

Hi, I would like to process, filter the same ASCII asynchronous live data stream in more than one pipe pipeline. So the one pipeline should filter out some records using grep key word and more than one pipes pipelines each should grep for another key words, each set seperately for each... (5 Replies)
Discussion started by: jack2
5 Replies

9. Shell Programming and Scripting

Duplicate output without tee

Hi, Is there anyway to duplicate output without using tee? Let me explain the problem. We are use ssh to login to remote server and save output to a file using tee commands for auditing purposes. When we use vi editor in ssh session, letters get garbled and cant really use vi. Without tee it... (7 Replies)
Discussion started by: eagles1
7 Replies

10. Shell Programming and Scripting

log script input and output using tee ?

hi, new to to forum... i've been trying to create a script in tcsh but i'm having a problem with one thing... the script has to keep log of it's input and output so i'm using tee -a log | script | tee -a log this keeps the logs as asked, but it gives me an extra empty prompt (not in the... (0 Replies)
Discussion started by: moseschrist
0 Replies
Login or Register to Ask a Question