2011-01-23 97 views
0

我有一個腳本A調用腳本G。內部腳本G我無法更改任何內容(不具有寫權限)。如何停止從腳本中退出?

腳本內G:

ExitProcess() 
{ 

    case $1 in 
    "0") echo "$0: Finished successfully." 
      exit 0 
     ;; 
*) echo "$0: Unknown return status ($1)" 
     exit $1 
     ;; 
esac 

} 

由於我是從腳本A退出,如何停止?

腳本A:

check_status() 
{ 

UserName="sbrk6" 
MachineName="sn26" 

Tstatus=`ssh -f -T ${UserName}@${MachineName} ps -ef | grep -w "Manager 1 PR" | egrep -v "grep|less|vi|more"` 
Cstatus=`ssh -f -T ${UserName}@${MachineName} ps -ef | grep -w "gt1" | egrep -v "grep|less|vi|more"` 

if [ "$Tstatus" ] 
then 
     if [ "$Cstatus" ] 
     then 

       Gstatus=`ps -ef | grep -w "Gth_Hndl" | egrep -v "grep|less|vi|more"` 

       if [ -z "$Gstatus" ] 
       then 
         genth_start 
       fi 
     fi 
else 

     if [ -z "$Tstatus" ] 
     then 
       if [ -z "$Cstatus" ] 
       then 

         Gstatus=`ps -ef | grep -w "Ghfdjdjd" | egrep -v "grep|less|vi|more"` 
         if [ "$Gstatus" ] 
         then 
           genth_stop 
         fi 
       fi 
     fi 
fi 
} 


genth_start() 
{ 
echo START 

. GD1_Sh 

} 


genth_stop() 
{ 
echo STOP 

. G_Sh 

##This is the Script G ### 

} 

while : 
do 
     check_status 
done 

想這循環繼續下去,直到我殺了這個劇本

回答

2

實施例的代碼。如果你不能改變腳本G,那麼它可能是最簡單的安排來運行它作爲一個單獨的過程,而不是使用.(點)的命令來運行它在當前的過程中。

如果腳本G只能在虛線模式下使用,那麼您可以編寫第三個腳本,將其命名爲腳本Z,該腳本用於運行腳本G並退出,而腳本A快樂地繼續(報告錯誤來自Z退出,然後做適當的事情)。

如果這也不可行,那麼你可以小心地使用eval$(...),這樣當腳本G中包含exit語句的函數實際退出時,它們在子shell中運行,而不在主shell中運行。這比單獨運行Script G要快很多,不管是否用Script Z包裝,所以我會首先使用其中一種機制。

0

您可以trap exit codes並調用特定的功能。在這種情況下,您不希望在腳本A中的函數結束時調用exit。

也可以在不使用exit的情況下使用return an integer。從this comment.

function return_code_test() 
{ 
return 50 
} 
+0

我沒有對在腳本(A)中調用的腳本(G)的權限。 – Kimi 2011-01-23 22:47:27

+0

編輯答案包括誘捕退出代碼 – Bryan 2011-01-23 22:54:28