Bash Basics
Bash script হলো একটা text file যেখানে terminal commands লেখা থাকে — একটার পর একটা। File-টা চালালে সব command ক্রমানুসারে execute হয়। Manual কাজ automate করার সবচেয়ে direct পথ।
Script কেন শিখবে
ধরো TryHackMe-তে একটা machine-এ 1 থেকে 254 পর্যন্ত সব IP-তে ping করতে হবে — কোনটা alive। হাতে হাতে ২৫৪টা command চালাবে?
# Script ছাড়া: ২৫৪ বার এই command
ping -c 1 192.168.1.1
ping -c 1 192.168.1.2
# ...
ping -c 1 192.168.1.254
# Script দিয়ে: একটাই file চালাও
./ping_sweep.sh 192.168.1এটাই scripting-এর শক্তি।
প্রথম Script
# নতুন script file তৈরি করো
nano hello.shScript-এর ভেতরে লেখো:
#!/bin/bash
echo "Hello, Omar!"
echo "আজকের তারিখ: $(date)"# Script-কে executable করো
chmod +x hello.sh
# চালাও
./hello.sh
# Hello, Omar!
# আজকের তারিখ: Sat Apr 11 17:00:00 UTC 2026Shebang (#!/bin/bash)
প্রতিটা bash script-এর প্রথম line হলো shebang।
#!/bin/bashএই line Linux-কে বলে: “এই file-টা /bin/bash দিয়ে চালাও।” ছাড়া দিলে Linux জানবে না কোন interpreter ব্যবহার করবে।
# bash কোথায় আছে দেখো
which bash
# /bin/bashVariables
Variable হলো একটা নাম যেটার আড়ালে কোনো value রাখা হয়।
#!/bin/bash
# Variable তৈরি করো — = এর দুই পাশে space দেবে না
name="Omar"
target="10.10.10.50"
port=80
# Variable ব্যবহার করো — $ দিয়ে
echo "Name: $name"
echo "Target: $target:$port"# Output:
# Name: Omar
# Target: 10.10.10.50:80Command-এর output variable-এ রাখো:
#!/bin/bash
# $(...) দিয়ে command-এর output ধরো
current_date=$(date)
my_ip=$(hostname -I | awk '{print $1}')
open_ports=$(ss -tlnp | grep LISTEN | wc -l)
echo "Date: $current_date"
echo "My IP: $my_ip"
echo "Open ports: $open_ports"User Input — read
Script চলার সময় user-এর কাছ থেকে input নাও।
#!/bin/bash
echo "Target IP দাও:"
read target_ip
echo "Port দাও:"
read port
echo "Scanning $target_ip:$port..."
nc -zv $target_ip $port# Prompt একই line-এ রাখতে
read -p "Target IP: " target_ip
read -p "Port: " portConditions — if
কোনো condition সত্য হলে কিছু করো।
#!/bin/bash
read -p "Port number দাও: " port
if [ $port -lt 1024 ]; then
echo "Well-known port — system service"
elif [ $port -lt 49152 ]; then
echo "Registered port — application"
else
echo "Dynamic/Private port"
fiComparison operators:
| Numeric | String | মানে |
|---|---|---|
-eq | = | সমান |
-ne | != | সমান নয় |
-lt | ছোট | |
-gt | বড় | |
-le | ছোট বা সমান | |
-ge | বড় বা সমান |
File check:
#!/bin/bash
if [ -f "/etc/passwd" ]; then
echo "File আছে"
fi
if [ -d "/tmp" ]; then
echo "Directory আছে"
fi
if [ -x "/usr/bin/nmap" ]; then
echo "nmap installed এবং executable"
fi| Flag | মানে |
|---|---|
-f | File আছে |
-d | Directory আছে |
-x | Executable |
-e | Exists (file বা dir) |
-z | Empty string |
Loops
for loop — নির্দিষ্ট সংখ্যকবার চালাও:
#!/bin/bash
# একটা list-এর প্রতিটা item
for user in root omar the-queen www-data; do
echo "Checking user: $user"
done
# Number range
for i in {1..10}; do
echo "Count: $i"
done
# IP range (ping sweep)
for ip in {1..254}; do
ping -c 1 -W 1 192.168.1.$ip &>/dev/null && echo "192.168.1.$ip is alive"
donewhile loop — condition সত্য থাকলে চালাও:
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Attempt $count"
count=$((count + 1))
doneFile-এর প্রতিটা line read করো:
#!/bin/bash
# wordlist.txt-এর প্রতিটা word দিয়ে কিছু করো
while read -r word; do
echo "Testing: $word"
done < wordlist.txtArguments
Script চালানোর সময় বাইরে থেকে value দাও।
#!/bin/bash
# $1 = প্রথম argument, $2 = দ্বিতীয়, $@ = সব
echo "Script নাম: $0"
echo "Target: $1"
echo "Port: $2"
echo "সব arguments: $@"
echo "মোট arguments: $#"./script.sh 10.10.10.50 80
# Script নাম: ./script.sh
# Target: 10.10.10.50
# Port: 80Argument validation:
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 <target-ip>"
exit 1
fi
target=$1
echo "Scanning $target..."Output Colors
Terminal output-কে color করো — পড়তে সহজ হয়।
#!/bin/bash
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color — reset
echo -e "${GREEN}[+] Success${NC}"
echo -e "${RED}[-] Failed${NC}"
echo -e "${YELLOW}[*] Info${NC}"CyberSec Note
Bash script — pentesting এবং CTF-এ:
Script-এ sensitive info রেখো না:
# ভুল — password hardcode করা
password="secret123"
# ঠিক — runtime-এ নাও
read -sp "Password: " password
# -s = silent (typing দেখাবে না)Target machine-এ script দেখলে বুঝবে কী:
# .sh files খোঁজো
find / -name "*.sh" 2>/dev/null
# Cron job-এ কোন script চলছে (scheduled tasks)
cat /etc/crontab
ls /etc/cron.d/
crontab -l
# যদি script world-writable হয় এবং root এটা চালায়
# সেটা privilege escalation এর সুযোগ
ls -la /etc/cron.d/backup.sh
# -rwxrwxrwx → যে কেউ edit করতে পারে → root হিসেবে চলবেQuick Check
- Shebang কী এবং কেন দরকার?
- Variable-এ value রাখতে
=এর দুই পাশে space দিলে কী হবে? -
$1মানে কী? -
if [ -f "/etc/passwd" ]— এটা কী check করে? - Script-কে চালানোর আগে কোন command দিতে হয়?
পরবর্তী → Automation Scripts