gpt4 book ai didi

Linux 如果 csh 中有退出代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:00 24 4
gpt4 key购买 nike

如何在 csh 的 if else 语句中传递失败脚本的退出代码?例如

if ( "test" != "test" ) then
/bin/true
else
/bin/false
endif

echo $?

给出0的退出代码。但是只是运行

/bin/false
echo $?

给出退出代码 1。

似乎 endif 只是覆盖了 if else 语句中命令输出的退出代码。

endif 
echo $?

退出代码:0

除了在运行每个命令后将其设置为变量之外,是否有更好的方法从语句中传递退出代码?

例如

if ( "test" != "test" ) then
/bin/true
set exitcode = $?
else
/bin/false
set exitcode = $?
endif

/bin/false 和/bin/true 只是这些位置的脚本示例,要么失败要么成功,以及 endif 如何覆盖它们的退出代码。因此,这些语句中的任何一个的退出代码都可以是任何 [0-255]。我不是要捕捉简单的 1 或 0。

更新

如果您创建一个 csh 脚本,Thomas 的以下建议似乎可行。

#!/bin/csh
if ( "test" != "test" ) then
/bin/true
exit $?
else
/bin/false
exit $?
endif

但是如果用ssh的话好像不行

ssh -A -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $USER@`hostname` '
>if ( "test" != "test" ) then
> /bin/true
> exit $?
>else
> /bin/false
> exit $?
>endif'
echo $?
0

最佳答案

你可以只使用退出:

#!/bin/csh
if ( "test" != "test" ) then
/bin/true
exit $?
else
/bin/false
exit $?
endif

exit可以处理1byte的退出码[0-255]

关于Linux 如果 csh 中有退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com