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.
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.