如何从 csh 脚本重定向 stdout 和 stderr

发布于 2024-08-13 03:35:11 字数 309 浏览 4 评论 0原文

对于以下脚本

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 和输出,而不要求用户进行重定向?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惜醉颜 2024-08-20 03:35:11

一些简单的解决方案:

解决方案 1:
tee 想要独立记录的每一行,使用 tee 的 -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

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

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

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
暖树树初阳… 2024-08-20 03:35:11

你好,

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

csh 中无法进行 stdio 操作。阅读“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

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

干杯,

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,

相对绾红妆 2024-08-20 03:35:11

您可以在子 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文