如果错误,退出tcsh脚本

ARM*_*ARM 11 shell tcsh

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

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

#!/usr/bin/env tcsh

set NAME=aaaa
set VERSION=6.1

#set -e equivalent
#do somthing
Run Code Online (Sandbox Code Playgroud)

谢谢

Mar*_*oij 11

在(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.
Run Code Online (Sandbox Code Playgroud)

所以你需要tcsh-e旗帜调用.我们来测试一下:

% cat test.csh
true
false

echo ":-)"

% tcsh test.csh
:-)

% tcsh -e test.csh
Exit 1
Run Code Online (Sandbox Code Playgroud)

没有办法在运行时设置它,就像使用sh's set -e,但你可以将它添加到hashbang:

#!/bin/tcsh -fe
false
Run Code Online (Sandbox Code Playgroud)

因此它会在您运行时自动添加./test.csh,但是在您键入时不会添加它csh test.csh,因此我的建议是使用类似于start.sh调用csh脚本的内容:

#!/bin/sh
tcsh -ef realscript.csh
Run Code Online (Sandbox Code Playgroud)