tee and functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting tee and functions
# 1  
Old 05-04-2012
tee and functions

Greetings!

My apologies if this has been answered elsewhere before. What I have is a function (as below) set up to append to either an error log or info log based upon input.

Code:
myLOGGER ()
	{
	if [[ $( echo "$1" | egrep -w error ) ]]; then
        logfile=$elog
        lastERROR="$1"  #used elsewhere in my script
    else
        logfile=$log
    fi 
	stamp=$(date +"%m/%d/%y %H:%M:%S")
	echo $stamp "$1" >>$logfile
	}


myLOGGER "my log message goes here"

This works perfectly, and does what I expect it to do. Whatever message I put in quotes is passed to the function and appended to the appropriate file.

My question is in whether I can use the command tee to output the same message to the screen and my function?

I know that I can do this:
Code:
echo "some text to display and log" | tee -a $log

What I'd like to do is something like the below, but I haven't found a way to do so.
Code:
echo "some text I want displayed and logged" | tee (myLOGGER)

I know that I can use tee within my function, however not everything I'm sending to the logs needs to be displayed as well. I suspect that I would just have to add another if statement and pass an additional argument to myLOGGER.
# 2  
Old 05-05-2012
Quote:
Originally Posted by reid
I know that I can use tee within my function, however not everything I'm sending to the logs needs to be displayed as well. I suspect that I would just have to add another if statement and pass an additional argument to myLOGGER.
That's the way I'd do it. Accept a "tty too" flag and echo before the remainder of your processing:

Code:
if [[ $1 == "-t" ]]
then
    shift 
    echo "$*"
fi

# rest of your log code here

call it like this to write to the tty in addition to the log:

Code:
myLOGGER -t "message text"


Last edited by agama; 05-05-2012 at 12:18 AM.. Reason: suggestion
This User Gave Thanks to agama For This Post:
# 3  
Old 05-05-2012
agama,

Took me a moment to decipher what you suggested, but it is perfect and simple. Two things I love in my scripts.

Thanks!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mindboggling difference between using "tee" and "/usr/bin/tee" in bash

I'm on Ubuntu 14.04 and I manually updated my coreutils so that "tee" is now on version 8.27 I was running a script using bash where there is some write to pipe error at some point causing the tee command to exit abruptly while the script continues to run. The newer version of tee seems to prevent... (2 Replies)
Discussion started by: stompadon
2 Replies

2. Shell Programming and Scripting

Help with tee command

In the current directory , I have seven files . But when I use the following command , it lists eight files ( 7 files + file_list.xtx) ls -1 | tee file_list.xtx | while read line; do echo $line ; done Does the tee command create the file_list.xtx file first and then executes the ls -1... (1 Reply)
Discussion started by: kumarjt
1 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Shell Programming and Scripting

tee + more command

script1: #!/bin/ksh more test.txt script2: calling the script1 #!/bin/ksh /tmp/script1.sh 2>&1 | tee tee.log where test.txt contains ~1200 lines. When I execute the script2 the more command does not print pagewise it goes to the end of the line, when I remove the tee command it... (4 Replies)
Discussion started by: prasad111
4 Replies

5. Shell Programming and Scripting

Using tee

I have been using the command tee to store the output to a file and also write on the terminal. However I would need to put the program in the background although I would still need to see the file being updated like it was doing when using tee. Any suggestions on how to look at the log file... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

tee

Someone recently advised me to use the tee command to write to standard out. Why would you pipe your commands to tee -a <filename> rather than just using >> <filename> ? For example: date|tee -a myfile seems to be the same as date >> myfile Is there a benefit to... (5 Replies)
Discussion started by: fracken_toaster
5 Replies

7. Shell Programming and Scripting

removing tee

Hi, I have a script where i want to log in details to the standard output as well as log file so that its easy for tracing purposes. I have used the "tee"command. The problem with this is my scripts lines are getting longer as for each line i have #!/bin/ksh echo "hello world" |... (4 Replies)
Discussion started by: pinnacle
4 Replies

8. UNIX for Dummies Questions & Answers

tee

hello how to append the hostname to each line of a file that is tee'd for example: tail -f file1 | tee file2 Iwant file2 to have the same new lines of file1 but with the hostname at the end or the beginning of each line. btw, is there more proper method than: tail -f file1 | tee... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

9. UNIX for Dummies Questions & Answers

How does tee work

Greetings to everybody. I would like to know if I can use the pipe and command tee to read from edited file and to write to him e.g. "sed '{s_A_B_}' file | tee file". :confused: I know it doesn't work with > but I don't know anything about it with tee. Thank you for your help. :) (1 Reply)
Discussion started by: Foxgard
1 Replies

10. UNIX for Dummies Questions & Answers

tee problem

Hi you, This the code I have: function show_menu { echo " lalalalal" echo " 1. ...." echo " 2. ...." echo " 3. exit" read choice case $choice in 1) ... ;; 2) ...;; *) stop=1;; esac } (5 Replies)
Discussion started by: bensky
5 Replies
Login or Register to Ask a Question