How to redirect stdout and stderr from csh script

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 the install.log file and console output without asking the user for a redirect?

+7
redirect csh
source share
3 answers

Some simple solutions:

Solution 1: on each line that you want to register yourself, use the -a tee switch to add

 #!/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 the current install.csh to install_commands, then add a new install.csh script file:

 #!/bin/csh -f /bin/csh install_commands |& tee install.log 
+7
source share

G'day

I highly recommend switching from csh to something like bash or zsh.

Stdio processing is not possible in csh. Read " csh programming is considered harmful ." An elegant treatise on the subject.

Sorry this is not a direct answer, but you will find that you will continue to bang your head about csh restrictions, the longer you stick to it.

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

Here is a short sentence for the same, written in bash. It is 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 added a little more checks and reports so that if a problem occurs at an earlier stage, the log file will simply contain until an error is detected, and not a stack of rather useless error messages from later phases, Anyway.

amuses

+3
source share

You can run it in a subshell and redirect all output. Do not remember if this works in csh, it has been a very long time since I used this.

 #!/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 
0
source share

All Articles