2013-03-10 5 views
9

सिंटैक्स बैश में कैसे काम करता है? यदि कथन के लिए यह सी शैली के लिए मेरा छद्म कोड है। उदाहरण के लिए:अगर नेस्टेड/फिर/elseif बैश में काम करता है तो कैसे घोंसला करता है?

If (condition) 
    then 
    echo "do this stuff" 

elseif (condition) 
    echo "do this stuff" 

elseif (condition) 
    echo "do this stuff" 

    if(condition) 
     then 
     echo "this is nested inside" 
    else 
     echo "this is nested inside" 

else 
    echo "not nested" 

उत्तर

25

मुझे लगता है कि आपके सवाल का कई व्याकरण में निहित झूलते किसी और अस्पष्टता के बारे में है, बाश में ऐसी कोई बात नहीं है। प्रत्येक if को किसी ब्लॉक fi द्वारा अवरुद्ध किया जाना चाहिए यदि ब्लॉक के अंत को चिह्नित किया गया हो।

इस तथ्य को देखते हुए (अन्य वाक्य रचनात्मक त्रुटियों के अलावा) आप देखेंगे कि आपका उदाहरण मान्य बैश स्क्रिप्ट नहीं है। कुछ त्रुटियों को ठीक करने का प्रयास करने से आपको

if condition 
    then 
    echo "do this stuff" 

elif condition 
    then 
    echo "do this stuff" 

elif condition 
    then 
    echo "do this stuff" 
    if condition 
     then 
     echo "this is nested inside" 
# this else _without_ any ambiguity binds to the if directly above as there was 
# no fi colosing the inner block 
    else 
     echo "this is nested inside" 

# else 
#  echo "not nested" 
# as given in your example is syntactically not correct ! 
# We have to close the last if block first as there's only one else allowed in any block. 
    fi 
# now we can add your else .. 
    else 
     echo "not nested" 
# ... which should be followed by another fi 
fi 
+0

बिल्कुल कुछ ठीक हो सकता है! धन्यवाद –

संबंधित मुद्दे