hexdefender
Email
  • Introduction to Linux
    • Overview
    • Linux Kernel
    • Linux Distros
    • Introduction to Kali Linux
    • Install Kali on VirtualBox
    • Install Kali on AWS
  • Linux Commands
    • Linux File Systems
    • Basic File and Directory commands
    • File Permissions and Ownerships
    • System Commands in Linux
    • Text Processing Commands in Linux
    • Linux Archive Utility
    • Package Management in Kali Linux
    • Networking Commands
    • Disk Utility Tools
    • Linux List of CLI Command lookup
    • Linux CLI Cheatsheet
    • Assignment
  • Networking Essentials
    • Overview
    • Networking Protocols
    • IP Addressing & Subnetting
    • DNS and DNS Security
    • Network Devices and Architecture
    • VPNs and Secure Tunnels
    • Network Address Translation (NAT) & Port Forwarding
    • Wireless Networks & Protocols
    • Cloud Networking & Security
    • Common Network Tools
  • Bash Scripting
    • Fundamentals of Bash
    • Variables, Branching and Loops
    • System Variables in Bash
    • Functions and Error Handling in Bash Scripts
    • File Handling and Text Processing
    • 5 Useful Bash Scripts for Everyday Tasks
    • Useful Assignments
  • Fundamentals of Cybersecurity
    • Introduction to Cybersecurity
    • Importance of Cybersecurity
    • Important Cybersecurity Frameworks
    • Cybersecurity Roles and Career Options
  • Penetration Testing
    • Reconnaissance and Footprinting
    • Exploitation Techniques
      • Introduction
      • Service Enumeration
      • Password Attacks
      • Exploit Discovery
      • The Art of Exploitation
      • The Pentester's guide to Metasploit
    • Post Exploitation - Malware & Escalation
  • Web Application Security
    • Common Web Vulnerabilities
    • OWASP Top 10
    • SQL Injections
    • Cross Site Scripting Attacks
    • Web Application Firewalls
    • Secure Coding Practices
  • Cryptography
    • Basic concepts of cryptography
    • Examples of Asymetric & Hashing functions
    • Public Key Infrastructure
    • Digital Signatures
    • Symmetric and Asymmetric Encryption
  • Social Engineering
    • Introduction to Social Engineering
    • Mitigation Strategies for Social Engineering
  • Digital Forensics
    • Digital Forensics Basics
    • Forensics Tools and Techniques
    • Reverse Engineering Fundamentals
    • Malware Analysis
Powered by GitBook
On this page
  1. Bash Scripting

Variables, Branching and Loops

Fundamentals of Variables, Conditional Branching, and Loops

Objectives:

  • Understand how to declare and use variables in Bash.

  • Learn how to use conditional statements (if, else, elif).

  • Explore loops (for, while) for iterating through data.

  • Apply these concepts in a real-world example.


2.1 Variables in Bash

Declaring Variables

In Bash, you can declare a variable by simply assigning a value to it. There is no need to specify the variable type.

# Example of variable declaration
name="Kunal"
age=30

Note: Do not add spaces around the = sign when assigning values.

Accessing Variables

To access the value of a variable, use the $ symbol:

echo "My name is $name and I am $age years old."

2.2 Conditional Branching

Conditional statements allow you to execute different actions based on certain conditions.

Using if, else, and elif

The syntax for conditional branching is as follows:

if [ condition ]; then
    # code to execute if condition is true
elif [ another_condition ]; then
    # code to execute if another_condition is true
else
    # code to execute if none of the above conditions are true
fi

Example: Checking Age

if [ $age -ge 18 ]; then
    echo "$name is an adult."
else
    echo "$name is a minor."
fi

2.3 Loops in Bash

Loops allow you to execute a block of code multiple times.

Using for Loops

A for loop can iterate over a list of items.

for i in {1..5}; do
    echo "Iteration $i"
done

Using while Loops

A while loop continues as long as a condition is true.

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

2.4 Real-World Example: User Registration

Let’s create a simple script that simulates a user registration process. The script will collect user information, check if the user is old enough to register, and provide feedback.

Example Script: user_registration.sh

#!/bin/bash

# User Registration Script

# Collect user information
echo "Enter your name:"
read name

echo "Enter your age:"
read age

# Check if the user is eligible to register
if [ $age -ge 18 ]; then
    echo "Welcome, $name! You are eligible to register."
else
    echo "Sorry, $name. You must be at least 18 years old to register."
    exit 1
fi

# Simulate account creation
echo "Creating your account..."
sleep 2  # Simulating a delay for account creation
echo "Account for $name created successfully!"

# Example of a loop to show registration steps
echo "Registration Steps:"
for step in "Fill out personal details" "Choose a username" "Set a password" "Confirm your registration"; do
    echo "- $step"
done

echo "Thank you for registering, $name!"

How to Run the Script

  1. Make the script executable:

    chmod +x user_registration.sh
  2. Run the script:

    ./user_registration.sh

2.5 Summary

In this chapter, you learned about:

  • Variables in Bash and how to declare and access them.

  • Conditional statements for making decisions based on conditions.

  • Loops for executing code multiple times.

  • A real-world example that combines these concepts in a user registration scenario.

Exercises:

  1. Modify the user registration script to include a username and validate its length.

  2. Add a loop to allow the user to register multiple accounts until they choose to exit.


Feel free to adjust any parts of the chapter or the example to better fit your teaching style or audience! Let me know if you need any further details or modifications.

PreviousFundamentals of BashNextSystem Variables in Bash

Last updated 8 months ago

https://github.com/bc0de0/bash-scripts/blob/master/user_registeration_sh