OSYMP_(2)[1] micro project - Read-Only.pptx

rohitmhaske0208 7 views 12 slides Mar 01, 2025
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

micro project


Slide Content

Introduction to Shell Scripting Shell scripting is a powerful tool for automating tasks and creating interactive command-line interfaces. It's a fundamental skill for system administrators, developers, and anyone who works with Linux or Unix-based systems. Shell scripts are essentially sequences of commands that are executed one after another, often using variables and conditional statements to make them more flexible and powerful.

Key Components Shell Scripting Shebang (#!): Specifies the shell to be used. Example: #!/bin/bash Comments: Used to explain parts of the script, starting with #. Example: # This is a comment Variables: Assign and access variables. Example: name="Piyush" and echo "Hello, $name“ Input and Output: Print output to the terminal and read user input.Example : echo "Enter your name: " and read name

Key Components Shell Scripting Conditional Statements: Use if, else, elif for decision- making.Example : if [ $name == "Piyush" ]; then echo "Welcome, Piyush!“ else echo "Hello, $name“ fi Loops: Use for, while, or until loops for repetitive tasks. Example: for i in {1..5} do echo "Looping ... number $ i “ done

Key Components Shell Scripting Functions: Define reusable blocks of code.Example : my_function () { echo "This is a function!“ } my_function File Permissions: Modify file permissions to make the script executable.Example : chmod +x script_name.sh Executing the Script: Run the script from the terminal.Example : ./script_name.sh

Printing Pyramid of Numbers One of the classic examples of using shell scripting for visual purposes is printing pyramids of numbers. These pyramids can have various patterns, with numbers ascending, descending, mirrored, or inverted. Let's explore some of these patterns and the shell scripts that generate them. 1 half pyramid A simple triangle of numbers aligned to the left. 2 inverted half pyramid A left-aligned triangle with decreasing rows of numbers. 3 full pyramid A symmetrical pyramid of numbers, centered. 4 Floyd’s Pyramid A triangular arrangement of numbers printed sequentially.

Pyramid Pattern 1: half pyramid In this pattern, In a half-pyramid, each row contains an increasing number of values, starting from 1 in the first row. The row number determines how many numbers are printed. #!/bin/bash # Number of rows for the pyramid rows=5 # Loop through the rows for (( i =1; i <=rows; i ++)) do # Inner loop for printing numbers in each row for ((j=1; j<= i ; j++ )) do echo -n "$j " done echo "" # Newline after each row done

Pyramid Pattern 2: full pyramid In a full pyramid, numbers form a symmetrical triangle with spaces for alignment. The row number controls how many values are printed, and each row is centered using spaces. #!/bin/bash rows=5 for (( i =1; i <=rows; i ++)) d o for ((j= i ; j<rows; j++ )) do echo -n " " # print space done for ((j=1; j<= i ; j++ )) do echo -n "$j" # print increasing numbers done for ((j=i-1; j>=1; j--)) do echo -n "$j" # print decreasing numbers done echo # move to the next line done

Pyramid Pattern 3: Floyd’s Pyramid Floyd’s Pyramid prints numbers consecutively row by row, starting from 1. The number of values in each row increases sequentially, forming a triangle. #!/bin/bash # Initialize variables count=1 rows=4 # Loop through the rows for (( i =1; i <=rows; i ++)) do # Inner loop to print numbers for ((j=1; j<= i ; j++ )) do echo -n "$count “ ((count++)) done echo done

Pyramid Pattern 4: inverted half pyramid In an inverted half-pyramid, the number of values in each row decreases as you move down. The top row has the most numbers, while the last row has only one. #!/bin/bash # Number of rows for the pyramid rows=5 # Loop for printing the decreasing pyramid for (( i =rows; i >=1; i --)) Do # Inner loop for printing numbers in each row for ((j=1; j<= i ; j++ )) do echo -n "$j " done echo "" # Newline after each row done

Advantages Shell Scripting Automation: Automates repetitive tasks, saving time and reducing errors. Efficiency: Runs commands quickly without needing to manually input them each time. Ease of Use: Simplifies complex command sequences into a single script. Portability: Shell scripts can run on any system with a compatible shell, making them versatile.

Disadvantages Shell Scripting Limited Power: Not as strong for complex tasks. Hard to Debug: Difficult to find and fix mistakes. Portability Issues: May not work on different systems without changes. Slower Performance: Can be slower than compiled programs.

Conclusion and Next Steps Shell scripting provides a powerful way to automate tasks and create creative outputs like pyramid patterns. By understanding basic shell commands and control structures, you can build scripts that meet your specific needs. Next, you can explore more complex patterns, experiment with different data types, or integrate your scripts with other tools and services.