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

标签 shell tcsh

我正在尝试编写一个 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/

相关文章:

linux - 如何删除图案线之间的所有线

linux - 如何通过管道查找结果来解压缩?

linux - 单列数据转多列

linux - 使用 find 命令但排除两个目录中的文件

linux - 在远程使用 ssh 命令列出选定文件时需要帮助

grep - 使用 grep 过滤 Doxygen 输出

bash 与 tcsh 别名参数传递

c - shell脚本 "Thread: command not found"

bash - 如何使用 xmllint 解析 xml 并将其存储在数组中

linux - 从 cshell (tcsh) 执行 bash shell