Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

There is a bash script which prints some logs and allows some arguments. The script prints the logs to STDOUT. Let's say the script's name is AAA.sh

I'd also like to make the script to print the logs to STDOUT and a file. This can be done with tee.

$ AAA.sh -a -b --c=d | tee 2012-07-03-080000.log

But my team often forgets to pipe the output to tee. We should save the logs to file. So I'd like to wrap the script with tee.

What I want to do is

$ WrapAAAwithTee.sh -a -b --c=d

Then AAAwithTee.sh should print the log to STDOUT and a log file.
How can I wrap the AAA.sh?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

This script will work better than the previous answer:

#!/bin/bash

exec AAA.sh "$@" | tee "$(date +'%F-%H%M%S').log"

This will work properly with spaces and give a unique name to the log file, based on the current time. The exec also makes it a little more efficient and removes the wrapper from your process tree, once the child has launched.

share|improve this answer

Your WrapAAAwithTee.sh should contain:

#!/bin/bash
AAA.sh "$@" | tee 2012-07-03-080000.log

tee will function normally inside a script, and "$@" will contain all parent arguments.

share|improve this answer
    
oh, thank you. Could you also know how to generate the datetime format dynamically? –  Benjamin Jul 3 '12 at 10:05
1  
Use ` ... | tee $(date +"%Y-%m-%d-%H%M%S").log`. If you want to have the log files all at at fixed directory, put an absolute path in front of the(generated) filename. –  jofel Jul 3 '12 at 10:23
4  
No no no no! Always use "$@" (with quotes) or you will have horrible problems as soon as you try a parameter with a space in it! –  ams Jul 3 '12 at 10:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.