Skip to main content

DAMN BASH

Bash Scripting: From Basic to Advanced

Bash Scripting: From Basic to Advanced

Bash (Bourne Again SHell) is a powerful command-line interpreter and scripting language commonly used in Linux and macOS environments. This post covers Bash scripting from basic commands to more advanced techniques.

I. Basic Commands

These commands are the building blocks of Bash scripting:

CommandDescription
lsLists files and directories.
cdChanges the current directory.
pwdPrints the current working directory.
mkdirCreates a new directory.
rmRemoves files or directories (use with caution!).
cpCopies files or directories.
mvMoves or renames files or directories.
catDisplays file content.
echoPrints text to the console.

II. Variables

Variables store data that can be used in your scripts:

name="John Doe"
echo "Hello, $name!"
age=30
echo $((age + 5)) # Arithmetic operations

III. Input/Output Redirection

Redirect input and output of commands:

OperatorDescription
>Redirects output to a file (overwrites existing content).
>>Appends output to a file.
<Redirects input from a file.
2>&1Redirects standard error to standard output.
ls -l > filelist.txt   # Save the output of ls -l to filelist.txt
cat < input.txt       # Read content from input.txt
command 2>/dev/null    # Suppress error messages

IV. Control Flow

Control the execution flow of your scripts:

If/Else Statements

if [ $age -gt 18 ]; then
  echo "You are an adult."
elif [ $age -gt 12 ]; then
    echo "You are a teenager."
else
  echo "You are a child."
fi

For Loops

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

for file in *.txt; do
    echo "Processing $file"
done

While Loops

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

V. Functions

Group code into reusable blocks:

greet() {
  echo "Hello, $1!"
}

greet "Alice"

VI. Advanced Topics

Regular Expressions (Regex)

Powerful pattern matching for text processing (using grep, sed, awk):

grep "^a.*z$" file.txt # Lines starting with 'a' and ending with 'z'

Process Management

Controlling running processes:

ps aux | grep process_name # Find a process
kill <PID>                  # Kill a process

Working with Arrays

my_array=("apple" "banana" "cherry")
echo ${my_array[0]}    # Access the first element
echo ${my_array[@]}    # Access all elements

VII. Example Script

#!/bin/bash

# A simple script to backup a directory

backup_dir="/path/to/backup"
target_dir="/path/to/target"
timestamp=$(date +%Y%m%d%H%M%S)
archive_file="$backup_dir/backup_$timestamp.tar.gz"

tar -czvf "$archive_file" "$target_dir"

echo "Backup created: $archive_file"

This is just a starting point. There are many more advanced features in Bash scripting. Explore the man bash pages for the full documentation.

Comments

Popular posts from this blog

Powershell Automation Basics - Part 1

Pentest Notes: PowerShell Automation - Basics Pentest Notes: PowerShell Automation - Basics These notes cover PowerShell automation for penetration testing, focusing on practical applications and techniques. What is PowerShell? A powerful scripting language and command-line shell built on the .NET framework, heavily integrated with Windows. Ideal for system administration and automation, making it a valuable tool for pentesters. Why PowerShell for Pentesting? Native to Windows: Pre-installed on most Windows systems. Object-oriented: Allows for complex data manipulation and interaction with APIs. Access to .NET Framework: Enables interaction with a vast library of classes and functions. Remoting capabilities: Execute commands on remote systems. Bypass security restrictions: Can be used to circumvent some security measures if not properly configured. Basic Syntax Cmdlets: Commands in PowerShell (e.g., Get-Process , Get-Service , Get-ChildItem ). P...

SQLDB Pentest

Pivoting for Red Teamers SQL Database & SQL Injection Pentesting Cheat Sheet SQL databases store crucial application data, and misconfigurations can make them vulnerable to SQL Injection (SQLi) attacks. This guide covers database enumeration, privilege escalation, and SQL injection techniques. Step 1: Identifying SQL Database Type Check the database type by sending payloads in the input fields or URL: ' OR 1=1 -- (MySQL, PostgreSQL, MSSQL) ' UNION SELECT 1,2,3 -- (Check column count) ' AND 1=CONVERT(int,@@version) -- (MSSQL Test) Observe the error messages for database identification. Step 2: Enumerating Database Tables & Columns Use SQL queries to extract database structure. For MySQL: SELECT table_name FROM information_schema.tables WHERE table_schema=DATABASE(); SELECT column_name FROM information_schema.columns WHERE table_name='user...