gpt4 book ai didi

shell - 如果出错则退出 tcsh 脚本

转载 作者:行者123 更新时间:2023-12-04 02:07:11 24 4
gpt4 key购买 nike

我正在尝试编写一个 tcsh 脚本。
如果任何命令失败,我需要脚本退出。

在 shell 中,我使用 set -e但我不知道它在 tcsh 中的等价物

#!/usr/bin/env tcsh

set NAME=aaaa
set VERSION=6.1

#set -e equivalent
#do somthing

谢谢

最佳答案

在 (t)csh, set用于定义变量; set foo = bar将分配值 bar到变量 foo (就像 foo=bar 在 Bourne shell 脚本中所做的那样)。

无论如何,来自 tcsh(1) :

Argument list processing
If the first argument (argument 0) to the shell is `-' then it is a
login shell. A login shell can be also specified by invoking the shell
with the -l flag as the only argument.

The rest of the flag arguments are interpreted as follows:

[...]

-e The shell exits if any invoked command terminates abnormally or
yields a non-zero exit status.

所以你需要调用 tcsh-e旗帜。让我们测试一下:
% cat test.csh
true
false

echo ":-)"

% tcsh test.csh
:-)

% tcsh -e test.csh
Exit 1

没有办法在运行时设置它,就像 shset -e ,但您可以将其添加到 hashbang:
#!/bin/tcsh -fe
false

所以当你运行 ./test.csh 时它会自动添加,但这会 不是 在输入 csh test.csh 时添加它,所以我的建议是使用类似 start.sh 的东西这将调用 csh脚本:
#!/bin/sh
tcsh -ef realscript.csh

关于shell - 如果出错则退出 tcsh 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com