2009-12-04 79 views
7

重定向輸出和錯誤對於下面的腳本如何從CSH腳本

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 

用途:

./install.csh |& tee install.log 

如何更改腳本,以便我仍然得到一個install.log和控制檯上的輸出,而不要求用戶重定向?

回答

7

一些簡單的解決方案:

解決方案1: 發球要獨立登錄,利用-a開關三通的追加

#!/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 

方案二每行:添加第二個腳本。 例如,當前install.csh重命名爲install_commands, 然後添加一個新install.csh腳本:

#!/bin/csh -f 
/bin/csh install_commands |& tee install.log 
+0

謝謝,我想有可能是一些其他的技巧,我不知道導致一個優雅的解決方案 – 2009-12-04 21:29:24

+0

Oooh。良好的呼叫分裂它到兩個腳本+1仍然不認爲你應該堅持使用csh!( - : – 2009-12-05 02:17:44

3

天兒真好,

強烈推薦從CSH對像bash或zsh中移開。

在csh中stdio操作是不可能的。閱讀「csh programming considered harmful」。關於這個話題的優雅論文。

對不起,這不是一個直接的答案,但你會發現,你將繼續撞你的頭,克服csh的限制,你堅持它的時間越長。

很多csh語法已經在bash中可用,所以你的學習曲線不會太陡峭。

下面是用bash編寫的同樣東西的一個快速建議。雖然這不是很優雅。

#!/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 

我更增添了幾分檢查和報告,這樣,如果有一個在前面的步驟的日誌文件將只包含直到錯誤被揭露,而不是從相當無用的錯誤消息的堆棧問題後來的階段不會完成。

歡呼聲,

+0

我該怎麼做bash? – 2009-12-04 21:31:47

+0

@Shelly,補充說。 ( - : – 2009-12-04 22:25:31

+0

@Shelly,oops。忘了你想複製到控制檯 – 2009-12-04 22:37:59

0

您可以在子shell中運行它和重定向的所有輸出。不記得這是否適用於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 
) |& tee install.log