加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:16:23 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个tcsh脚本. 如果任何命令失败,我需要脚本退出. 在shell中我使用set -e但我不知道它在tcsh中的等价物 #!/usr/bin/env tcshset NAME=aaaaset VERSION=6.1#set -e equivalent#do somthing 谢谢 在(t)csh中,set用于定义变量; set foo = bar会将
我正在尝试编写一个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(就像Boo shell脚本中的foo = bar一样).

在任何情况下,从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.

所以你需要用-e标志调用tcsh.我们来试试吧:

% 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读