我正在尝试编写一个 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
没有办法在运行时设置它,就像
sh
的 set -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/