tee vs output redirection confusion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tee vs output redirection confusion
# 1  
Old 03-21-2009
Question tee vs output redirection confusion

ok,
suppose i have a file called f1
Code:
[c_d@localhost pipesnfilters]$ cat 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

and i have another file f2
Code:
[c_d@localhost pipesnfilters]$ cat f2
this is file2

the task is to eliminate the repeated lines in f1 and add the content of f2 into f1
so according to the task given my projected output is something like this
Code:
[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

notice that the contents of f1 and f2 are merged into f1 and the repeated line "this is file1" has an entry only once.

Now, I have accomplished the task using the following piece of code
Code:
[c_d@localhost pipesnfilters]$ cat f1 f2 | sort -u | tee f1

the quick brown fox jumped over the lazy dog
this is file1
this is file2
this is unix
who let the dogs out

but suppose i use the following code to perform my task, i dont get the required output.
Code:
[c_d@localhost pipesnfilters]$ cat f1 f2 | sort -u >f1
[c_d@localhost pipesnfilters]$ cat f1
this is file2

All I am doing in the above is displaying contents of f1 and f2 and filtering out the repeated lines and redirecting this output to the file f1...so how come only content of file f2 is stored in f1?
i mean the output of
Code:
[c_d@localhost pipesnfilters]$ cat f1 f2 | sort -u

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


the quick brown fox jumped over the lazy dog
this is file1
this is file2
this is unix
who let the dogs out

isnt this supposed to be redirected into the file f1???
but why isnt this the case. why do i get my file f1 as
Code:
this is file2

and tee is
Code:
[c_d@localhost pipesnfilters]$ whatis tee
tee []               (1p)  - duplicate standard input
tee []               (1)  - read from standard input and write to standard output and files
tee []               (2)  - duplicating pipe content

so how come tee works but redirection does not work here?

I wish to know whats going on here.
what actually do the makers of unix mean when they say redirection and what they mean when they say duplication?

thanks in advance.
# 2  
Old 03-21-2009
This happens because the redirection has higher precedence than the rest of the operations so in the second case the file f1 is emptied before the cat command executes. If you want to achieve the same with redirection you'll need something like this:

Code:
{ rm f1&&cat - f2|sort -u>f1;}<f1

# 3  
Old 03-21-2009
Quote:
Originally Posted by c_d
I wish to know whats going on here.
what actually do the makers of unix mean when they say redirection and what they mean when they say duplication?
Redirection is changing which file a program uses to read or write. Duplication, as used in the context of the tee program, means reading some input and writing that input several times to (we presume) different files.

Quote:
Originally Posted by radoulov
Code:
{ rm f1&&cat - f2|sort -u>f1;}<f1

Smilie Twisted but brilliant. I'm not sure if I love it or hate it... but that's a hallmark of great art.

To clarify: when you do:
somecommand > file
the shell forks a copy of itself to run the command. This copy opens "file" for writing, truncating it to empty if it already exists. Then the copy exec's "somecommand". By the time somecommand starts to run, the output file is already empty.
# 4  
Old 03-21-2009
a variation on a theme with the sed (lacking '-i'):
Code:
{ rm FILE; sed -e '...' > FILE; } < FILE

# 5  
Old 03-22-2009
Quote:
Originally Posted by Perderabo
Redirection is changing which file a program uses to read or write. Duplication, as used in the context of the tee program, means reading some input and writing that input several times to (we presume) different files.


Smilie Twisted but brilliant. I'm not sure if I love it or hate it... but that's a hallmark of great art.
ok i need help understanding the following points in the command by radoulov
1.why was rm f1 issued and then && applied?

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...

3. cat - f2 <f1 i understood, but i could'nt grasp the order in which the commands would execute?
to further explain my confusion....this is what i could make out of the command
Code:
[c_d@localhost pipesnfilters]$ { rm f1 && cat - f2 | sort -u >f1 ; } <f1

here, the code inside the brackets gets executed first, but first since redirection takes precedence - is read from f1 . then a logical is performed between the outputs of rm f1 and cat - f2? I am not really sure of that though...because replacing && with ; also works. so i think its executing 2 different commands one after the other, instead of performing a logical operation...please clarify whats happening here....

{ rm f1 && cat - f2 ; } <f1 gives the following output...
Code:
[c_d@localhost pipesnfilters]$ { rm f1 && cat - f2 ; } <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

this is file2

with file f1 deleted
and this is piped with sort -u so , the repeated lines would be eliminated...
Code:
[c_d@localhost pipesnfilters]$ { rm f1 && cat - f2 | sort -u ;} <f1

the quick brown fox jumped over the lazy dog
this is file1
this is file2
this is unix
who let the dogs out

then you are redirecting the output to f1 using >f1 it does not matter what the contents of f1 , as f1 is removed...

please correct me...Smilie clearly i m very confused...
how is it that in this version <f1 percedes >f1???? and no matter however i try to manipulate this command i get the wrong output?



Quote:
To clarify: when you do:
somecommand > file
the shell forks a copy of itself to run the command. This copy opens "file" for writing, truncating it to empty if it already exists. Then the copy exec's "somecommand". By the time somecommand starts to run, the output file is already empty.
when you say exec's "somecommand" you dont mean the exec command do you,you meant executes "somecommand" didnt you? ofcourse you dont...but if at all you do, then i did not understand your clarification...Smilieif you didnt then i did...Smilie
# 6  
Old 03-22-2009
command1 && command2

First, command1 is run. Then if it does an "exit 0" or equivalent, then command2 is run. If command1 reports that it failed, then we just stop.

Shells are written in C and they use system calls to do stuff. fork() and exec() are system calls. The exec() system call is the model for the exec shell command. I was referring to the system calls being invoked.
# 7  
Old 03-22-2009
Quote:
Originally Posted by Perderabo
command1 && command2

First, command1 is run. Then if it does an "exit 0" or equivalent, then command2 is run. If command1 reports that it failed, then we just stop.

Shells are written in C and they use system calls to do stuff. fork() and exec() are system calls. The exec() system call is the model for the exec shell command. I was referring to the system calls being invoked.
ok, that actually makes sense...

but what seems like art to you, is like cipher text to me...Smilie please explain radoulov's command...
 
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