9

对于以下脚本

安装.csh:

#!/bin/csh -f
tar -zxf Python-3.1.1.tgz
cd Python-3.1.1
。/配置
制作
进行安装
光盘..
rm -rf Python-3.1.1

有可能的使用:

./install.csh |& tee install.log

如何更改脚本,以便在不要求用户进行重定向的情况下仍然在控制台上获得 install.log 和输出?

4

3 回答 3

6

一些简单的解决方案:

解决方案 1: tee 独立记录的每一行,利用-atee 的 switch 来追加

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

解决方案 2:添加第二个脚本。例如,将当前 install.csh 重命名为 install_commands,然后添加新的 install.csh 脚本:

#!/bin/csh -f 
/bin/csh install_commands |& tee install.log
于 2009-12-04T20:34:05.830 回答
3

天,

强烈建议从 csh 转向 bash 或 zsh 之类的东西。

stdio 操作在 csh 中是不可能的。阅读“被认为有害的 csh 编程”。关于这个主题的优雅论文。

抱歉,这不是一个直接的答案,但您会发现,您坚持使用 csh 的时间越长,您就会一直在努力克服 csh 的限制。

bash 中已经提供了很多 csh 语法,因此您的学习曲线不会太陡峭。

这是用 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

我添加了更多检查和报告,以便如果在前面的步骤中出现问题,日志文件将只包含直到发现错误,而不是来自后面阶段的一堆非常无用的错误消息,这些错误消息无法完成反正。

干杯,

于 2009-12-04T20:30:24.153 回答
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
于 2010-11-04T18:07:56.483 回答