Script की Structuring (Functions, main() Pattern, अंत में Exit)
In this page:
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
#!/bin/bash
greet() {
echo "Hello, $1"
}
farewell() {
echo "Goodbye, $1"
}
greet "Ada"
farewell "Ada"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
आखिर में 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
#!/bin/bash
main() {
echo "doing the real work"
return 0
}
main "$@"
exit $?
Login to try C/C++/Java/PHP code in the editor
एक Script को Testing के लिए Sourceable बनाना
main call को ऐसे guard करना कि यह सिर्फ तब run हो जब script directly execute हो (sourced होने पर नहीं) दूसरी scripts या एक test harness को आपके functions load करने देता है बिना पूरे program को automatically trigger किए।
BASH_SOURCE[0] == $0 guard Python के if __name__ == "__main__": का bash equivalent है।उदाहरण: Making a Script Sourceable for Testing
#!/bin/bash
greet() {
echo "Hello, $1"
}
main() {
greet "World"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Login to try C/C++/Java/PHP code in the editor
- एक script को बिना किसी functions के top-level commands की एक long sequence के रूप में लिखना, किसी individual piece को test या reuse करना hard बनाते हुए।
- एक
mainfunction define करना पर script के bottom पर इसे actually call करना भूल जाना। - 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 बनाता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script