首页 \ 问答 \ 在测试退出状态的上下文中调用时,set -e在函数中被忽略了吗?(set -e ignored in function when invoked in context where exit status is tested?)

在测试退出状态的上下文中调用时,set -e在函数中被忽略了吗?(set -e ignored in function when invoked in context where exit status is tested?)

我试图退出shell函数,只要其中运行的任何命令都使用set -e失败

foo () (
    set -e
    echo $SHELLOPTS
    false
    echo set -e failed
)

echo test1:
foo
test $? -ne 0 && echo died

echo
echo test2:
foo && echo died

这是我使用bash 4.2.37获得的输出:

test1:
braceexpand:errexit:hashall:interactive-comments
died

test2:
braceexpand:errexit:hashall:interactive-comments
set -e failed
died

如果将foo称为简单命令( test1 ),则fooecho命令之前退出,正如我所期望的那样。 但是,如果在复合命令( test2 )中调用,则似乎忽略set -e并继续执行失败命令。

我知道除了管道的最后一个元素之外,除了-e故障之外的所有部分都会被忽略,但我不希望如果子管道作为管道的一部分运行,它的exiterr状态将被覆盖,特别是如果明确设置的话。 请注意, SHELLOPTS表示即使在第二次测试中也始终设置errexit

我错过了什么吗?

谢谢,

迪亚布


I'm attempting to exit out of a shell function whenever any of the commands run in it fail using set -e

foo () (
    set -e
    echo $SHELLOPTS
    false
    echo set -e failed
)

echo test1:
foo
test $? -ne 0 && echo died

echo
echo test2:
foo && echo died

Here's the output I get using bash 4.2.37:

test1:
braceexpand:errexit:hashall:interactive-comments
died

test2:
braceexpand:errexit:hashall:interactive-comments
set -e failed
died

If foo is called as a simple command (test1), foo exits before the echo command, as I expect. However, if called in a compound command (test2), the set -e seems to be ignored and execution continues past the failing command.

I know that with -e failures are ignored for all but the last element of a pipeline, but I don't expect that if a subshell is run as part of a pipeline that its exiterr status would be overridden especially if explicitly set. Note that SHELLOPTS indicates that errexit is always set, even in the second test.

Am I missing something?

Thanks,

Diab


原文:https://stackoverflow.com/questions/35706525
更新时间:2021-11-26 22:11

最满意答案

你可以使用.siblings()

$("li").on("click", function(){ 
  $(this).siblings().removeClass('active');
  $(this).addClass("active");
});
li:hover{
  background-color: #eee;
  cursor: pointer;
}

.active{
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>One</li>
  <li class="active">Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
</ul>


You can use .siblings()

$("li").on("click", function(){ 
  $(this).siblings().removeClass('active');
  $(this).addClass("active");
});
li:hover{
  background-color: #eee;
  cursor: pointer;
}

.active{
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>One</li>
  <li class="active">Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
</ul>

相关问答

更多
  • getElementsByClassName是DOM文档上的一个函数。 它既不是jQuery也不是jqLite函数。 使用它时,不要添加类名之前的期间: var result = document.getElementsByClassName("multi-files"); 将其包装在jqLite中(如果jQuery在Angular之前加载,则为jQuery): var wrappedResult = angular.element(result); 如果要从指令的链接函数中的element中选择,则需 ...
  • 你可以使用.siblings() $("li").on("click", function(){ $(this).siblings().removeClass('active'); $(this).addClass("active"); }); li:hover{ background-color: #eee; cursor: pointer; } .active{ color: red; }