2015-09-25 59 views
2

我有一個csh腳本,需要使用newgrp進行部分運行。爲此,它遞歸地調用它自己。如何查看在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塊,但我仍然好奇。

回答

1

變量$status正在被這裏的文檔裏的外殼擴展。所以當newgrp外殼運行它看到if (0 != 0) then而不是if ($status != 0) then(因爲if,或任何was successful immediately before the newgrp`命令)。

要麼你需要躲避$的這裏文檔中:

if (\$status != 0) then 

或here文檔字的報價部分,以防止擴大從完全發生:

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'