← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 3 of 7

Script की Structuring (Functions, main() Pattern, अंत में Exit)

एक script को clearly named functions में organize करना, एक main function के साथ जो उन्हें साथ जोड़ता है, इसे एक long list of commands से कहीं ज़्यादा पढ़ना और maintain करना आसान बनाता है।
Syntax
bash
function_one() {
  commands
}

main() {
  function_one
}

main "$@"
exit 0

एक Long Script vs. Small Functions

Logic को छोटे, named functions में split करना -- हर एक एक clear task के लिए responsible -- एक script की overall structure को एक glance में obvious बनाता है और हर piece को independently testable बनाता है।

उदाहरण: One Long Script vs. Small Functions

bash
#!/bin/bash
greet() {
    echo "Hello, $1"
}

farewell() {
    echo "Goodbye, $1"
}

greet "Ada"
farewell "Ada"

main() Pattern

एक main function define करना जो बाकी functions को सही order में call करता है, और main "$@" को script की बिल्कुल last line के रूप में invoke करना, पूरी script को एक clear, obvious entry point देता है, C में main या Python के if __name__ == "__main__" जैसा।

उदाहरण: The main() Pattern

bash
#!/bin/bash
setup() {
    echo "setting up..."
}

run_task() {
    echo "running task: $1"
}

cleanup() {
    echo "cleaning up..."
}

main() {
    setup
    run_task "$1"
    cleanup
}

main "demo-task"

आखिर में Explicitly Exit करना

main को इसके अपने return status का उपयोग करके एक explicit exit से खत्म करना script के final exit code को intentional और ढूंढना easy बनाता है, जो भी एक deeply nested function के अंदर last command ने return किया उस पर rely करने के बजाय।

उदाहरण: Exiting Explicitly at the End

bash
#!/bin/bash
main() {
    echo "doing the real work"
    return 0
}

main "$@"
exit $?

एक Script को Testing के लिए Sourceable बनाना

main call को ऐसे guard करना कि यह सिर्फ तब run हो जब script directly execute हो (sourced होने पर नहीं) दूसरी scripts या एक test harness को आपके functions load करने देता है बिना पूरे program को automatically trigger किए।

Note: यह BASH_SOURCE[0] == $0 guard Python के if __name__ == "__main__": का bash equivalent है।

उदाहरण: Making a Script Sourceable for Testing

bash
#!/bin/bash
greet() {
    echo "Hello, $1"
}

main() {
    greet "World"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक script को बिना किसी functions के top-level commands की एक long sequence के रूप में लिखना, किसी individual piece को test या reuse करना hard बनाते हुए।
  2. एक main function define करना पर script के bottom पर इसे actually call करना भूल जाना।
  3. Logic को directly top level पर function definitions के साथ interleaved रखना, script के overall flow को एक glance में follow करना hard बनाते हुए।
चैप्टर सारांश
  • एक script को छोटे, purpose-named functions में तोड़ना हर piece को पढ़ना, test करना, और reuse करना easier बनाता है।
  • सभी functions define होने के बाद, बिल्कुल bottom पर एक main "$@" call script को एक clear entry point देता है।
  • सभी function definitions को साथ रखना, फिर एक short "orchestration" section, mirror करता है कि बाकी languages programs को कैसे structure करती हैं।
  • यह structure एक script को किसी दूसरी script या interactive shell में individual functions testing के लिए source करना भी easier बनाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.