Alekhya varma
Alekhya varma

Reputation: 95

Tee doesn't get the output from the pipe

I am trying to output to a file through Tee,But it isn't working. Surprisingly it used to work before, But now it doesn't. Please tel me the problem or help me with an alternative code.

#!/bin/bash
{
dirname=/path
exec > path/logfile.log 2>&1
tempfile=myTempfileName
find $dirname -type f  > $tempfile
cat $tempfile | sed 's_.*/__' | sort |  uniq -d |
while read fileName
do
 grep "$fileName" $tempfile
done
} | tee 'path/scripts/tj_var1.txt'
#| awk -F"/" '{print $NF}'  | tee 'path/scripts/tj_var1.txt' | sort -u | tee 'path/scripts/tj_mail1.txt'

The log shows the output is being generated. But somehow the it doesn't pass on to tee command output. i could see tee is trying to write something, by the time stamp of the file. But there is nothing in the file.

enter image description here

Upvotes: 0

Views: 1278

Answers (1)

KamilCuk
KamilCuk

Reputation: 141890

If you redirect the output to exec > path/logfile.log then... well, the output will be redirected to the file, not to the pipe.

Try:

#!/bin/bash
{
   dirname=/path
   tempfile=myTempfileName
   find "$dirname" -type f > "$tempfile"
   sed 's_.*/__' "$tempfile" | sort | uniq -d |
   while read fileName
   do
      grep "$fileName" "$tempfile"
   done
} 2>&1 | tee -a path/logfile.log | tee 'path/scripts/tj_var1.txt'
#            ^^ I guess log file should be appended.

I guess you could have only stdout in the tj_var1.txt file:

#!/bin/bash
{
{
   dirname=/path
   tempfile=myTempfileName
   find "$dirname" -type f > "$tempfile"
   sed 's_.*/__' "$tempfile" | sort | uniq -d |
   while read fileName
   do
      grep "$fileName" "$tempfile"
   done
} | tee 'path/scripts/tj_var1.txt'
} 2>&1 | tee -a path/logfile.log

It basically finds out the duplicate filenames and outputs it.

Just:

dirname=/path
find "$dirname" -type f -printf "%f\n" |
sort | uniq -d |
tee -a path/logfile.log | tee 'path/scripts/tj_var1.txt'

Upvotes: 1

Related Questions