Control stucture in shell programming in Linux in hindi

Shell scripting mein control structures (control statements) ka istemal flow control aur decision-making ke liye hota hai. Niche diye gaye important control structures hain:

Control stucture in shell programming in Linux in hindi

1. if Structure in Linux in hindi:

if structure ka istemal conditions ko check karne aur execution flow ko control karne ke liye hota hai. Syntax:

if [ condition ]
then
    # Commands to execute if condition is true
fi

Example:

age=20
if [ $age -ge 18 ]
then
    echo "You are an adult."
fi

2. Nested if Structure in Linux in hindi:

Nested if structure, ek if structure ke andar doosra if structure hota hai. Yeh multiple conditions ko test karne ke liye istemal hota hai.

Example:

if [ $age -ge 18 ]
then
    if [ "$gender" == "Male" ]
    then
        echo "You are an adult male."
    else
        echo "You are an adult, but not male."
    fi
fi

3. if.. then.. elif Structure in Linux in hindi:

if.. then.. elif structure multiple conditions ko test karne ke liye hota hai. Agar pehli condition false hoti hai, to dusri condition test hoti hai, aur aise hi chalti rahti hai jab tak koi condition true na ho.

Example:

if [ $age -lt 18 ]
then
    echo "You are a minor."
elif [ $age -ge 18 -a $age -lt 60 ]
then
    echo "You are an adult."
else
    echo "You are a senior citizen."
fi

4. case Structure in Linux in hindi:

case structure multiple conditions ko test karne ke liye hota hai, jaise ki switch-case statement in other programming languages.

Example:

case "$day" in
    "Monday")
        echo "It's Monday."
        ;;
    "Tuesday" | "Wednesday")
        echo "It's either Tuesday or Wednesday."
        ;;
    *)
        echo "It's another day."
        ;;
esac

5. for, while, aur until Statements in Linux in hindi:

  • for loop ka istemal lists ya sequences ke elements ko iterate karne ke liye hota hai.
  • while loop ek condition true hone tak execute hota hai.
  • until loop ek condition true hone tak execute hota hai, lekin condition opposite hoti hai while loop ke comparison mein.

Example of for loop:

for i in 1 2 3 4 5
do
    echo "Number: $i"
done

Example of while loop:

count=1
while [ $count -le 5 ]
do
    echo "Count: $count"
    count=$((count + 1))
done

Example of until loop:

count=1
until [ $count -gt 5 ]
do
    echo "Count: $count"
    count=$((count + 1))
done

In control structures ka istemal shell scripts mein decision-making aur repetitive tasks ko control karne ke liye kiya jata hai. Aap in structures ka istemal karke scripts ko dynamic aur functional bana sakte hain.

Leave a Comment