我有一个csh脚本需要使用newgrp来运行它的一部分。为此,它递归地调用自己。
#!/bin/csh
if ( $1 == "" ) then
newgrp << ENDGRP
./newgrp_test.csh 2
if ( $status != 0 ) then
echo "Second run fail"
exit 1
else
echo "Second run success"
exit 0
endif
ENDGRP
if ( $status != 0) then
echo "Exiting failure"
exit 1
else
echo "Exiting success"
exit 0
endif
endif
if ( $1 == "2" ) then
exit 1
endif
问题是我认为应该保证失败,但我得到了成功的输出。我的输出如下:
Second run success
Exiting success
为什么我不能在newgrp中读取脚本的状态?
注意我已经通过删除newgrp和ENDGRP之间的if块找到了解决方法,但我仍然很好奇。
答案 0 :(得分:1)
The variable $status
is being expanded by the outer shell inside the here document. So when the newgrp
-spawned shell runs it is seeing if ( 0 != 0 ) then
and not if ( $status != 0 ) then
(because the if
, or whateverwas successful immediately before the
newgrp` command).
You either need to escape the $
in the here document:
if ( \$status != 0 ) then
or quote part of the here document word to prevent expansion from happening entirely:
newgrp << 'ENDGRP'
./newgrp_test.csh 2
if ( $status != 0 ) then
echo "Second run fail"
exit 1
else
echo "Second run success"
exit 0
endif
'ENDGRP'