1846721 Members
3709 Online
110256 Solutions
New Discussion

scripting

 
SOLVED
Go to solution
Troyan Krastev
Regular Advisor

scripting

Hi All,

The script:
_______________________
#!/usr/bin/sh
echo "Try" 2>&1 >/tmp/log
_______________________
is aquivalnt of:
_______________________
#!/usr/bin/sh
exec 2>&1
exec 1>/tmp/log
echo "Try"
_______________________
How can I modify the next one to use "exec" too?
_______________________
#!/usr/bin/sh
echo "Try" 2>&1 | tee -a /tmp/log_tee >/tmp/log
_______________________
I want to avoid using "2>&1 | tee -a /tmp/log_tee >/tmp/log"
after every "echo" command!

Thanks,
Troy
6 REPLIES 6
Rodney Hills
Honored Contributor
Solution

Re: scripting

Depending on how interactive your script is, you could put parenthesis around the entire script-

example-
#!/usr/bin/sh
(
echo "start of my process"
./myprocess
echo "end of my process"
) | tee -a /tmp/log_tee >/tmp/log

Another method is to use the "script" command to log all output (and input).

example-
#!/usr/bin/sh
script /tmp/log_tee
echo "start of my process"

HTH

-- Rod Hills
There be dragons...
David Burgess
Esteemed Contributor

Re: scripting

exec is a global redirect. So once you have redirected stdout and stderr to /tmp/log all output goes there. If you then tee it you will get 2 entries in /tmp/log.

The simplest way would be to

tail -f /tmp/log

in a different window.

Regards,

Dave.
Steven E. Protter
Exalted Contributor

Re: scripting

I'm not exactly sure here but this might save keystrokes at least.

TMAN="2>&1 | tee -a /tmp/log_tee >/tmp/log"

echo "stuff" $TMAN

If the code does the job, why are we messing with it?

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Michael Schulte zur Sur
Honored Contributor

Re: scripting

Hi,

you can bundle consequtive echos in a function.

func1()
{
echo "Start"
echo "doing something"
echo "finished"
}

func1 2>&1 | tee -a /tmp/log_tee

The other alternative would define
O=2>&1 | tee -a /tmp/log_tee >/tmp/log
echo "Try" $O

BTW, what's the use of two copies in two files?

Michael
curt larson_1
Honored Contributor

Re: scripting

exec 2>&1
tee -a /tmp/log_tee >/tmp/log |&
exec 1>&p

echo "try"
Troyan Krastev
Regular Advisor

Re: scripting

Thank you all,

All these work. Now it is up to me to choose a solution.
Steven: I have seen alot of ugly scripts in my live. They work, but it doesn't meen anything.
Michael: The real script does't use 2 log file. One of them is /dev/console
Curt: Your solution is interesting.

Thanks again,
Troy