BCA / B.Tech 11 min read

Sub Program in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Sub Program in Hindi - General Topics
  • • अवलोकन: Quick Answer: Linux Shell Scripting में Subprogram को Function कहा जाता है। यह reusable code का एक स्वतंत्र block होता है जिसे shell script में एक नाम द...
  • • मुख्य अवधारणाएं: Sub program, Functions parameters return Hindi, Recursion sub,Sub program, Functions parameters return, Recursion sub
Quick Answer: Linux Shell Scripting में Subprogram को Function कहा जाता है। यह reusable code का एक स्वतंत्र block होता है जिसे shell script में एक नाम दिया जाता है और आवश्यकतानुसार बार-बार call किया जा सकता है। यह code duplication को रोकता है, debugging को आसान बनाता है, और script को modular structure offer करता है।

Subprogram (फंक्शन) क्या है? (What is a Shell Function?)

जब हम Linux में complex shell scripts लिखते हैं, तो कई tasks (जैसे file check करना, backup लेना, input validation) बार-बार दोहराए जाते हैं। यदि एक ही code को बार-बार लिखा जाए, तो script बहुत लंबी और error-prone हो जाती है। इसके समाधान के लिए Subprograms यानी Functions का use किया जाता है।

Linux Operating System और command-line tools की बुनियादी समझ के लिए आप linux in Hindi और commands of linux in Hindi देख सकते हैं। Shell functions script के अंदर ही execute होते हैं, इसलिए इनके execution में नया child process create नहीं होता और system overhead बहुत कम रहता है।

Advertisement

लिनक्स में Function की Syntax (Defining Functions in Bash)

Bash shell में function को दो तरीकों से define किया जा सकता है:

तरीका 1: Standard POSIX Syntax (सबसे अनुशंसित)

function_name() {
    # commands to execute
    # code statements
}

तरीका 2: function Keyword Syntax

function function_name {
    # commands to execute
}

Calling a Function: Function को call करने के लिए parentheses () लगाने की जरूरत नहीं होती; केवल function का नाम और arguments लिखे जाते हैं: function_name arg1 arg2।

Special Parameters और Arguments Handling

Shell functions में arguments को command-line arguments की तरह ही positional parameters से access किया जाता है। Exam में पूछे जाने वाले special variables नीचे table में दिए गए हैं:

Variable Meaning / Description Example
$1, $2, ... $9 Function में pass किए गए 1st, 2nd, ... 9th arguments. $1 पहला argument hold करता है।
$# Function में कुल pass किए गए arguments की संख्या (count)। if [ $# -lt 2 ]; then ...
$@ सभी arguments को individual quoted strings के रूप में expand करता है। for item in "$@"; do ...
$* सभी arguments को एक single string में combine करता है। "$*" IFS separator के साथ।
$? पिछली command या function का exit status (0 = success, non-zero = error)। echo "Exit status: $?"

Local vs Global Variables (चर का कार्यक्षेत्र)

By default, Bash shell में सभी variables Global होते हैं। यदि आप function के अंदर किसी variable की value बदलते हैं, तो वह पूरे script में बदल जाती है। इससे बचने के लिए local keyword का उपयोग किया जाता है:

#!/bin/bash
var="Global Value"

my_func() {
    local var="Local inside function"
    echo "Inside: $var"
}

my_func
echo "Outside: $var"

Output:
Inside: Local inside function
Outside: Global Value

Functions से Value Return करना: return vs echo

Linux Shell Scripting में function से value प्राप्त करने के दो अलग तरीके होते हैं:

Mechanism Data Returned How to Capture in Script
return [0-255] केवल numeric exit status code (0 से 255 तक)। इसका उपयोग success/failure बताने के लिए होता है। Function call के तुरंत बाद $? check किया जाता है।
echo "output" Strings, बड़ी संख्याएं, lists या complex output data। Command Substitution से: result=$(function_name)।

Worked Code Example: Factorial और File Checker

नीचे एक practical executable Bash script दी गई है जो दो subprograms (functions) का उपयोग करती है:

#!/bin/bash

# Function 1: File Existence Checker
check_file() {
    local filepath="$1"
    if [ -f "$filepath" ]; then
        echo "File '$filepath' exists."
        return 0
    else
        echo "Error: File '$filepath' not found!"
        return 1
    fi
}

# Function 2: Factorial Calculator using Loop
calculate_factorial() {
    local num=$1
    local fact=1

    while [ $num -gt 1 ]; do
        fact=$((fact * num))
        num=$((num - 1))
    done

    # Return large number via echo
    echo $fact
}

# --- Main Script Execution ---
echo "=== Subprograms Demonstration in Linux ==="

# 1. Calling File Checker
check_file "/etc/passwd"
status=$?
echo "Check file exit code: $status"

# 2. Calling Factorial Calculator
input_num=5
factorial_result=$(calculate_factorial $input_num)
echo "Factorial of $input_num is: $factorial_result"

Explanation: check_file function success/failure के लिए return 0 और return 1 use करता है जिसे $? से पढ़ा गया। calculate_factorial math output को echo करता है जिसे command substitution $(...) से capture किया गया। जब processes आपस में data share करते हैं, तो OS level पर inter process communication in Hindi की तकनीकों का उपयोग होता है।

विद्यार्थी अक्सर क्या गलतियां करते हैं? (Common Student Mistakes)

  • Function call में Parentheses लगाना: छात्र अक्सर C/Java की तरह my_func(arg1, arg2); लिख देते हैं। Bash में यह syntax error देता है; call हमेशा my_func arg1 arg2 होना चाहिए।
  • Return statement से बड़ा number लौटाने की कोशिश: यदि आप return 300 करेंगे, तो $? में modulo 256 होकर 44 मिलेगा। Numbers या strings लौटाने के लिए हमेशा echo का use करें।
  • Local keyword न लगाना: Function के अंदर loop counter (जैसे i=1) को local न बनाने से main script का loop variable override हो जाता है और गलत result या infinite loop बन सकता है।

Exam के लिए Important Questions

Short Questions (2 से 5 अंक)

  1. Linux Shell Scripting में Subprogram (Function) क्या है? इसका basic syntax लिखिए।
  2. Shell function में $# और $@ का क्या अर्थ होता है?
  3. Function के अंदर local keyword का उपयोग क्यों जरूरी है?
  4. Shell function में return command की सीमा (0-255) क्या है? बड़ी value कैसे लौटाते हैं?
  5. Function और Standalone Shell Script में क्या अंतर है?

Long Questions (10 से 20 अंक)

  1. Linux Bash scripting में Functions के concept को detail में समझाइए। Positional parameters, local variables और value returning mechanisms को code examples के साथ स्पष्ट कीजिए।
  2. एक interactive Bash script लिखिए जिसमें menu-driven options हों (जैसे Prime Number check करना, Factorial निकालना, और File search करना) और प्रत्येक कार्य के लिए अलग-अलग subprograms बनाए गए हों।
  3. Shell functions की memory efficiency, execution speed और modular programming के लाभों का विश्लेषण कीजिए।

FAQs

Q1: क्या Bash में Recursive Functions बनाए जा सकते हैं?

हाँ, Bash function स्वयं को call कर सकता है। लेकिन recursion depth को limit करना चाहिए ताकि memory stack overflow न हो।

Q2: क्या एक function को दूसरे script में import किया जा सकता है?

हाँ, source ./functions.sh या . ./functions.sh लिखकर किसी external file में मौजूद functions को current script में import किया जा सकता है।

Q3: Shell Function और Alias में क्या अंतर है?

Alias केवल एक command का छोटा shortcut होता है जिसमें complex logic या arguments validation नहीं हो सकता। Function में loops, conditions और multiple statements शामिल हो सकते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Linux Shell Scripting में Subprogram को Function कहा जाता है।
Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स के पाठ्यक्रम में Sub Program in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Sub Program in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Sub Program in Hindi
Distributed System in Hindi
Real Time System in Hindi
System Calls in Hindi
System Program in Hindi
Structure of Operating System in Hindi
Layers Design of Structure in Hindi
Unix in Hindi
Virtual Machine in Hindi
Kernel-Based Operating System in Hindi
Process in Hindi – OS में Process क्या है
Interacting Processes in Hindi
Threads in Hindi
Fundamental of Schedulling in Hindi
Scheduling Criteria in Hindi
Long Medium Short Term Scheduling in Hindi
Scheduling Algorithms in Hindi
Structure of Concurrent System in Hindi
Critical Region in Hindi
Critical Section in Hindi
Inter process Communication in Hindi
Monitor in Hindi
Semaphore in OS in Hindi – Counting और Binary Semaphore
Semaphores Implementation & Uses in Hindi
Logical and Physical Address in Hindi
Swapping in Hindi
Contiguous Allocation in Hindi
Segmentation in Hindi
Paging in OS in Hindi – Paging और Segmentation
Paging with Segmentation in Hindi
kernel Memory Allocation in Hindi
Page Replacement Algorithms in Hindi
Virtual Memory in Hindi
Virtual Memory with demand Paging in Hindi
Deadlock & Characterization in Hindi
Methods for Handling Deadlocks in Hindi
Deadlock Thrashing in Hindi
Allocation of Frames in Hindi
Virtual Memory using Segmentation in Hindi
Interprocess Communication protocol in Hindi
Network Operating System in Hindi
Design Issues in Distributed File System in Hindi
Network Structure in Hindi
Structure Distributed System in Hindi
File System and Coordination in Hindi
Linux in Hindi
Commands of Linux in Hindi | लिनक्स के कमांड्स हिंदी में
Programmer Interface & File manipulation in Hindi
Process control in Linux in Hindi
Linux Kernel & Signals in Hindi
File System in Linux in Hindi
block & inodes in Hindi
System Editor in Hindi
Character Transliteration in Hindi
ED in Hindi
Vi Editor & There Commands in Hindi
Shell Script in Hindi
Looping & Decision Making in Hindi
Variables & File name Expansion in Hindi
Array in Hindi OS
C interface with Linux in Hindi
Simpel Shell Programs in Hindi
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is Operating System (OS) in Hindi
Operating System (OS) All Important Questions and Answers in Hindi (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Type of Operating System in Hindi
Goals of Operating System in Hindi
Operation of Operating system in Hindi
Resource Allocation & Functions in Hindi
class of Operating system in Hindi
Batch Processing in Hindi
Multi Processing in Hindi
Time Sharing in Hindi