SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
Intermediate Shell Scripting
Loop in Script
● Loop -Automates repeated actions to streamline script execution
● Example: Creates a basic 1-5 counting loop that prints numbers sequentially
● Output: Displays "Iteration X" where X is the current loop number (1-5)
Loop Syntax
● The script opens with "#!/bin/bash", identifying it as a Bash script.
● Syntax: The loop structure follows the format: 'for i in {1..5}; do', ending with
'done'
● Loop Purpose: Executes code repeatedly through iterations
● Syntax for 'for' loop:
Uses 'for i in {start..end}; do action; done'
● chmod +x loop.sh - Makes scripts executable
● ./loop.sh - The script executes by printing numbered outputs in
sequence.
While Loops
● To display numbers 1 through 5 sequentially
● The script begins with "#!/bin/bash", indicating it's a Bash script.
● Syntax: The loop uses 'while [ $count-le 5 ]; do', with 'done' as its closing
statement.
● Loop Purpose: Executes code repeatedly until count reaches 5
● Syntax for 'while' loop: Uses 'while [condition]; do action; done'
● Output: The script prints each count value in sequence, incrementing by 1
● chmod +x while_loop.sh -grants execution permissions
● ./while_loop.sh -This script executes the script to display counting from 1
to 5.
Shell Script Function
● Reusable blocks of code that help organize and modularize scripts.
● Improve readability and maintainability by avoiding redundant code.
● Syntax : function_name() { # Commands}
● Parameters are passed like arguments in scripts: $1, $2, ..., $N represent
positional parameters.
● The script begins with "#!/bin/bash", indicating it's a Bash script.
● Function Definition: Uses 'functionName() { commands; }' structure
● Function Call: Executes as 'functionName argument'
● chmod +x function.sh -Makes the function script executable by granting
execution permissions.
● ./function.sh - Executes the function script which outputs "Hello, User!" as
greeting message.
Shell Script Input Validation
● The script prompts for user input and stores it in 'num' variable for checking
if it's a valid number.
● Using regex pattern matching and if-else logic, it validates the input and
displays appropriate success or error message.
● The script begins with "#!/bin/bash", indicating it's a Bash script.
● Input Structure: Uses 'read-p "prompt" variable' to capture user input
● Validation Syntax: Implements 'if [[ $variable =~ pattern ]]; then action; else
error; fi'
● Example Implementation: Shows number validation using 'if [[ $num =~
^[0-9]+$ ]]' with appropriate success/error messages
● chmod +x input.sh -Makes the input validation script executable by
granting execution permissions.
● ./input.sh - Executes the input validation script which prompts for a
number and validates if it's numeric.
● Shell scripts handle files using commands like touch, cat, cp, mv, and rm.
● Redirection (>, >>) and condition checks ([-f file ]) help manage files
efficiently.
● Example: Create a file, write text into it, and display its contents
Shell Script File Operations
File Operation Script Syntax
● The script begins with "#!/bin/bash", indicating it's a Bash script.
● File Creation: Uses 'touch filename' to create an empty file
● File Writing: Implements 'echo "text" > filename' to write content
● File Reading: Uses 'cat filename' to display file contents
● chmod +x file.sh - The script requires execution permissions to run.
● ./file.sh -The script creates, writes to, and displays a test file's contents.

More Related Content

DOCX
What is a shell script
PDF
Operating_System_Lab_ClassOperating_System_2.pdf
PPTX
Shell Programming Language in Operating System .pptx
PPT
2-introduction_to_shell_scripting
PPT
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
PPT
Shell programming
PPT
ShellAdvanced shell scripting programm.ppt
What is a shell script
Operating_System_Lab_ClassOperating_System_2.pdf
Shell Programming Language in Operating System .pptx
2-introduction_to_shell_scripting
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
Shell programming
ShellAdvanced shell scripting programm.ppt

Similar to Shell Scripting Intermediate - RHCSA+.pdf (20)

DOCX
sguide to write shell script in linux.docx
PPT
Unix shell scripting basics
PPT
Unix Shell Scripting Basics
PDF
Shell Programming_Module2_Part2.pptx.pdf
PPTX
Bash Shell Scripting
PDF
Linux shell script-1
PPT
ShellProgramming and Script in operating system
PPTX
Linux System Administration
PPTX
Shell & Shell Script
PPTX
Shell & Shell Script
PPTX
Linux Shell Scripting
PDF
Shell scripting _how_to_automate_command_l_-_jason_cannon
PPT
34-shell-programming asda asda asd asd.ppt
PPTX
Bash Shell Scripting
PPTX
Scripting 101
PPT
390aLecture05_12sp.ppt
PDF
Module 03 Programming on Linux
PPTX
Unix Shell Programming subject shell scripting ppt
PPT
34-shell-programming.ppt
sguide to write shell script in linux.docx
Unix shell scripting basics
Unix Shell Scripting Basics
Shell Programming_Module2_Part2.pptx.pdf
Bash Shell Scripting
Linux shell script-1
ShellProgramming and Script in operating system
Linux System Administration
Shell & Shell Script
Shell & Shell Script
Linux Shell Scripting
Shell scripting _how_to_automate_command_l_-_jason_cannon
34-shell-programming asda asda asd asd.ppt
Bash Shell Scripting
Scripting 101
390aLecture05_12sp.ppt
Module 03 Programming on Linux
Unix Shell Programming subject shell scripting ppt
34-shell-programming.ppt
Ad

More from RHCSA Guru (20)

PDF
Flatpak CLI Application Management & Building on RHEL.pdf
PDF
How to Set Up a RHEL 9 Virtual Lab for RHCSA Practice.pdf
PDF
CompTIA Linux (Plus) Exam Overview .pdf
PDF
Linux Professional Institute LPIC-1 Exam.pdf
PDF
LVM Management & Disaster Recovery - RHCSA+.pdf
PDF
Managing Multiple Logical Volumes - RHCSA+.pdf
PDF
Master Logical Volume Management - RHCSA+.pdf
PDF
Ansible Vault Encrypting and Protecting Secrets - RHCE.pdf
PDF
Ansible Variables in Playbook - RHCE.pdf
PDF
Comprehensive Guide to Ansible Application Roles.pdf
PDF
Using Tags in Ansible Playbooks RHCE.pdf
PDF
Comprehensive Guide to Ansible Roles - RHCE
PDF
Mastering Handlers, Conditions, and Loops in Ansible Playbooks - RHCE.pdf
PDF
Essential Ad-hoc Commands in Ansible - RHCE.pdf
PDF
RHEL in Shell Scripting Advanced - RHCSA+.pdf
PDF
Introduction to Shell Scripting - RHCSA+.pdf
PDF
Command Line Text Processing - RHCSA +.pdf
PDF
Mounting and Creating Links - RHCSA (RH124) .pdf
PDF
Basic Command Line Tools - RHCSA (RH124).pdf
PDF
Podman Networking Essentials - RHCSA+.pdf
Flatpak CLI Application Management & Building on RHEL.pdf
How to Set Up a RHEL 9 Virtual Lab for RHCSA Practice.pdf
CompTIA Linux (Plus) Exam Overview .pdf
Linux Professional Institute LPIC-1 Exam.pdf
LVM Management & Disaster Recovery - RHCSA+.pdf
Managing Multiple Logical Volumes - RHCSA+.pdf
Master Logical Volume Management - RHCSA+.pdf
Ansible Vault Encrypting and Protecting Secrets - RHCE.pdf
Ansible Variables in Playbook - RHCE.pdf
Comprehensive Guide to Ansible Application Roles.pdf
Using Tags in Ansible Playbooks RHCE.pdf
Comprehensive Guide to Ansible Roles - RHCE
Mastering Handlers, Conditions, and Loops in Ansible Playbooks - RHCE.pdf
Essential Ad-hoc Commands in Ansible - RHCE.pdf
RHEL in Shell Scripting Advanced - RHCSA+.pdf
Introduction to Shell Scripting - RHCSA+.pdf
Command Line Text Processing - RHCSA +.pdf
Mounting and Creating Links - RHCSA (RH124) .pdf
Basic Command Line Tools - RHCSA (RH124).pdf
Podman Networking Essentials - RHCSA+.pdf
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Understanding_Digital_Forensics_Presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
sap open course for s4hana steps from ECC to s4
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx

Shell Scripting Intermediate - RHCSA+.pdf

  • 2. Loop in Script ● Loop -Automates repeated actions to streamline script execution ● Example: Creates a basic 1-5 counting loop that prints numbers sequentially ● Output: Displays "Iteration X" where X is the current loop number (1-5)
  • 3. Loop Syntax ● The script opens with "#!/bin/bash", identifying it as a Bash script. ● Syntax: The loop structure follows the format: 'for i in {1..5}; do', ending with 'done' ● Loop Purpose: Executes code repeatedly through iterations ● Syntax for 'for' loop: Uses 'for i in {start..end}; do action; done'
  • 4. ● chmod +x loop.sh - Makes scripts executable ● ./loop.sh - The script executes by printing numbered outputs in sequence.
  • 5. While Loops ● To display numbers 1 through 5 sequentially
  • 6. ● The script begins with "#!/bin/bash", indicating it's a Bash script. ● Syntax: The loop uses 'while [ $count-le 5 ]; do', with 'done' as its closing statement. ● Loop Purpose: Executes code repeatedly until count reaches 5 ● Syntax for 'while' loop: Uses 'while [condition]; do action; done' ● Output: The script prints each count value in sequence, incrementing by 1
  • 7. ● chmod +x while_loop.sh -grants execution permissions ● ./while_loop.sh -This script executes the script to display counting from 1 to 5.
  • 8. Shell Script Function ● Reusable blocks of code that help organize and modularize scripts. ● Improve readability and maintainability by avoiding redundant code. ● Syntax : function_name() { # Commands} ● Parameters are passed like arguments in scripts: $1, $2, ..., $N represent positional parameters.
  • 9. ● The script begins with "#!/bin/bash", indicating it's a Bash script. ● Function Definition: Uses 'functionName() { commands; }' structure ● Function Call: Executes as 'functionName argument'
  • 10. ● chmod +x function.sh -Makes the function script executable by granting execution permissions. ● ./function.sh - Executes the function script which outputs "Hello, User!" as greeting message.
  • 11. Shell Script Input Validation ● The script prompts for user input and stores it in 'num' variable for checking if it's a valid number. ● Using regex pattern matching and if-else logic, it validates the input and displays appropriate success or error message.
  • 12. ● The script begins with "#!/bin/bash", indicating it's a Bash script. ● Input Structure: Uses 'read-p "prompt" variable' to capture user input ● Validation Syntax: Implements 'if [[ $variable =~ pattern ]]; then action; else error; fi' ● Example Implementation: Shows number validation using 'if [[ $num =~ ^[0-9]+$ ]]' with appropriate success/error messages
  • 13. ● chmod +x input.sh -Makes the input validation script executable by granting execution permissions. ● ./input.sh - Executes the input validation script which prompts for a number and validates if it's numeric.
  • 14. ● Shell scripts handle files using commands like touch, cat, cp, mv, and rm. ● Redirection (>, >>) and condition checks ([-f file ]) help manage files efficiently. ● Example: Create a file, write text into it, and display its contents Shell Script File Operations
  • 15. File Operation Script Syntax ● The script begins with "#!/bin/bash", indicating it's a Bash script. ● File Creation: Uses 'touch filename' to create an empty file ● File Writing: Implements 'echo "text" > filename' to write content ● File Reading: Uses 'cat filename' to display file contents
  • 16. ● chmod +x file.sh - The script requires execution permissions to run. ● ./file.sh -The script creates, writes to, and displays a test file's contents.