Skip to content

chauhan-pratham/Bash-Scripting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Bash Scripting for Cybersecurity & Automation

Table of Contents

  1. Introduction
  2. Hello World
  3. Variables
  4. User Input
  5. Positional Arguments
  6. If-Else Statements
  7. Switch Case
  8. Functions
  9. Piping & Redirection
  10. Exit Codes
  11. AWK Basics
  12. SED (Stream Editor) Basics

1. Introduction

This repository contains Bash scripting examples for automation, cybersecurity, and system administration.


2. Hello World

#!/usr/bin/bash
echo "Hello World!"
  • Run the script:
    chmod u+x hello.sh  
    ./hello.sh  

3. Variables

Defining and Using Variables

#!/usr/bin/bash
First_Name="Pratham"
Last_Name="Chauhan"
echo "Hello $First_Name $Last_Name"

4. User Input

#!/usr/bin/bash

echo -n "Enter your name: "
read name

echo "Hello, $name!"

5. Positional Arguments

#!/usr/bin/bash
echo "Hello $1 $2"
  • Run:
    ./script.sh Pratham Chauhan
    Output:
    Hello Pratham Chauhan
    

6. If-Else Statements

#!/usr/bin/bash

if [ "${1,,}" = "pratham" ]; then
    echo "Welcome Back Boss"
elif [ "${1,,}" = "help" ]; then
    echo "Enter your username"
else
    echo "You are not my boss"
fi

7. Switch Case

#!/usr/bin/bash

case "${1,,}" in 
    pratham | administrator)
        echo "Hello boss"
        ;;
    help) 
        echo "Enter your username"
        ;;
    *) 
        echo "You are not the boss"
        ;;
esac

8. Functions

Basic Function

#!/usr/bin/bash

showname(){
    echo "Hello $1"
}

showname Pratham

9. Piping and Redirection

ls -l /usr/bin | grep zsh
echo "Hello Pratham" > hello.txt   # Overwrites file  
echo "Hello Chauhan" >> hello.txt  # Appends to file  

10. Exit Codes

[ 200 -eq 100 ]  
echo $?   # Returns 1 (false)  

[ 200 -eq 200 ]  
echo $?   # Returns 0 (true)  

11. AWK Basics

awk '{print $1}' testfile.txt   # Prints first word from each line  
awk '{print $2}' testfile.txt   # Prints second word from each line  

12. SED (Stream Editor) Basics

SED (Stream Editor) is a powerful tool for text manipulation.

Basic Find and Replace

sed 's/Pratham/He/g' sedtest.txt

Before (sedtest.txt)

Hello everyone my name is Anonymous. I am learning bash now.  
Pratham is a good boy. Pratham can do everything.  
Thank you that's from Pratham's side.  

After Running the Command

Hello everyone my name is Anonymous. I am learning bash now.  
He is a good boy. He can do everything.  
Thank you that's from He's side.  

Delete a Specific Line

sed '3d' file.txt

Delete Lines Containing a Word

sed '/error/d' logfile.txt

Replace Text in a File (In-Place)

sed -i 's/oldtext/newtext/g' filename.txt

Extract Specific Lines

sed -n '2,4p' filename.txt

Remove Empty Lines

sed '/^$/d' file.txt

How to Use These Scripts

  1. Clone the repository:
    git clone https://github.com/yourusername/bash-scripting.git
    cd bash-scripting
  2. Make scripts executable:
    chmod u+x script.sh
  3. Run scripts:
    ./script.sh

Contributing

Feel free to contribute by adding more examples or improving existing ones.

🚀 Happy Scripting! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published