English 中文(简体)
How to redirect stdout and stderr from csh script
原标题:
  • 时间:2009-12-04 20:09:58
  •  标签:
  • redirect
  • csh

For the following script

install.csh:

#!/bin/csh -f
tar -zxf Python-3.1.1.tgz
cd Python-3.1.1
./configure
make
make install
cd ..
rm -rf Python-3.1.1

Intended use:

./install.csh |& tee install.log

How can I change the script so that I still get a install.log and the output on console without asking the user to do the redirecting?

最佳回答

Some simple solutions:

Solution 1: tee every line you want to log independently, make use of -a switch of tee to append

#!/bin/csh -f    
tar -zxf Python-3.1.1.tgz |& tee -a install.log
cd Python-3.1.1 |& tee -a install.log
./configure |& tee -a install.log
make |& tee -a install.log
make install |& tee -a install.log
cd .. |& tee -a install.log
rm -rf Python-3.1.1 |& tee -a install.log

Solution 2: Add a second script. For example, rename current install.csh to install_commands, then add a new install.csh script:

#!/bin/csh -f 
/bin/csh install_commands |& tee install.log
问题回答

G day,

I highly recommend moving away from csh towards something like bash or zsh.

stdio manipulation is not possible in csh. Have a read of "csh programming considered harmful". An elegant treatise on this topic.

Sorry it s not a direct answer but you ll find that you ll keep banging your head against the constraints of csh the longer you stick with it.

A lot of csh syntax is already available in bash so your learning curve won t be too steep.

Here s a quick suggestion for the same thing written in bash. It s not elegant though.

#!/bin/bash
TO_LOGFILE= "| tee -a ./install.log"
tar -zxf Python-3.1.1.tgz 2>&1 ${TO_LOGFILE}
if [ $? -ne 0 ];then
    echo "Untar of Python failed. Exiting..."; exit 5
fi

cd Python-3.1.1 2>&1 ${TO_LOGFILE}
if [ $? -ne 0 ];then
    echo "Can t change into Python dir. Exiting..."; exit 5
fi
echo "============== configure ================"
./configure 2>&1 ${TO_LOGFILE}
if [ $? -ne 0 ];then
    echo "Configure failed. Exiting..."; exit 5
fi
echo "================ make ==================="
make 2>&1 ${TO_LOGFILE}
if [ $? -ne 0 ];then
    echo "Compile of Python failed. Exiting..."; exit 5
fi
echo "================ install ================"
make install 2>&1 ${TO_LOGFILE}
if [ $? -ne 0 ];then
    echo "Install of Python failed. Exiting..."; exit 5
fi

cd ..
rm -rf Python-3.1.1 2>&1 ${TO_LOGFILE}
exit 0

I ve added a bit more checking and reporting so that if there s a problem in an earlier step the log file will just contain up until the error was uncovered rather than a stack of pretty useless error messages from the later phases that wouldn t complete anyway.

cheers,

You can run it in a subshell and redirect all the output of that. Don t remember if this works in csh, it has been a long, long time since I used that.

#!/bin/csh -f
(
tar -zxf Python-3.1.1.tgz
cd Python-3.1.1
./configure
make
make install
cd ..
rm -rf Python-3.1.1
) |& tee install.log




相关问题
How to use redirect_to to a non-Rails URL with query params?

We just had an existing use of redirect_to break due to a Rails upgrade, and it led to a question. I ve been experimenting, and I don t seem to find a way to use redirect_to to send the user to a non-...

Apache authentication: Redirect on failure, reliably?

I ve set my ErrorDocument 401 to point to my website s account creation page, but not all browsers seem to honor this redirect (Safari). Also, other browsers (Firefox, Chrome) never quit asking for ...

Response.Redirect HTTP status code

Why is it that ASP/ASP.NET Response.Redirect uses a HTTP-302 status code ("Moved Temporarily") even though in most cases a HTTP-301 status code ("Moved Permanently") would be more appropriate?

Exceptions: redirect or render?

I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...

Cakephp is not redirecting properly when pages are cached

I am having some issues with a site that was working correctly until i implemented full page caching in CakePHP. I have followed the guidance in the Manual and have my $session->flash in a no-cache ...

热门标签