Linux如果退出代码在csh中

时间:2018-02-23 09:18:05

标签: linux if-statement ssh csh

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

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

echo $?

给出退出代码0.但是只是运行

/bin/false
echo $?

退出代码为1.

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

endif 
echo $?

退出代码:0

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

e.g。

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

1 个答案:

答案 0 :(得分:1)

您可以使用退出:

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

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