2015-05-14 68 views
0

我希望控制流轉至已處理的代碼。是他們沒有辦法做到這一點 一些代碼 ... ... 標籤 一些代碼 ... ... GOTO標籤在ksh中使用GOTO返回已處理的代碼

基本上我想要一個循環,它之後可以再次執行已經處理過一次。基於一定條件

+0

對不起,你可以編輯你的答案,使其更容易理解。你用,幾乎所有的語言循環,檢查一些條件,達到相同的目的,你使用goto和標籤。並請提及語言。 –

+0

嗨Abhinav ...我必須這樣做是在ksh腳本 –

+0

好吧我沒有意識到像ksh存在的東西:D –

回答

1

您沒有轉到,您需要使用其他流控制來實現它。 一個小例子,你可以評論主後啓動,啓動與功能正常處理正常,當some_condition等於2

您可以將下面的腳本保存到一個文件(gupta.sh)重新啓動正常,文件模式它(chmod +x gupta.sh )並將其稱爲10次(./gupta.sh)以查看不同的隨機執行並修改。

#!/bin/ksh 

function get_random { 
    ((between_0_3 = ${RANDOM} % 4)) 
    echo "Random return= ${between_0_3}" 
    return ${between_0_3} 
} 

function normal { 
     echo "Start normal execution" 
     get_random 
     # Store the return value in a var 
     some_condition=$? 
     # ksh has a switch with the case keyword 
     case ${some_condition} in 
     0) 
      echo "All went well" 
      return 0 
      # Syntax in case: finish with 2 ; characters 
      ;; 
     1) 
      echo "OOPS, return error to main" 
      return 1 
      ;; 
     2) echo "This is the situation you want to restart (kind-of-goto)." 
      return 2 
      ;; 
     *) echo "Unexpected situation, stop without returning to main." 
      exit 1 
      ;; 
     esac 
} 

# main 
while [ 1 ]; do 
    normal 
    if [ $? -ne 2 ]; then 
     break; 
    fi 
done 
echo "End of main" 

這不是流量控制的最佳示例,而是一個如何嘗試實施goto的示例。你應該練習for-和while循環。