Ultimate Scripting Guide: Bash & PowerShell (Beginner to Advanced)
1. Introduction to Scripting
What is a script? A script is a file containing a sequence of commands that are executed by a command-line
interpreter.
Why use Bash or PowerShell? - Bash: Popular in Linux/Unix systems, great for automation and server
management. - PowerShell: Native to Windows, object-oriented, powerful for system administration.
Setting up your environment: - Linux or WSL for Bash - PowerShell installed on Windows (latest version
recommended)
2. Bash Basics
2.1 Creating Your First Script
#!/bin/bash
# This is a comment
echo "Hello, World!"
- Save as [Link] and run:
chmod +x [Link]
./[Link]
2.2 Variables and Data Types
name="Spud"
age=25
echo "My name is $name and I am $age years old"
2.3 Basic Operators
a=5
b=3
sum=$((a + b))
echo $sum
1
2.4 User Input and Output
read -p "Enter your name: " username
echo "Hello, $username!"
2.5 Conditional Statements
read -p "Enter a number: " num
if [ $num -gt 10 ]; then
echo "Number is greater than 10"
else
echo "Number is 10 or less"
fi
2.6 Loops
For loop:
for i in {1..5}; do
echo "Iteration $i"
done
While loop:
count=1
while [ $count -le 5 ]; do
echo $count
((count++))
done
2.7 Functions
function greet() {
echo "Hello, $1"
}
greet "Spud"
3. PowerShell Basics
3.1 Writing Your First Script
2
# Hello.ps1
Write-Output "Hello, World!"
- Run: .\Hello.ps1
3.2 Variables and Types
$name = "Spud"
$age = 25
Write-Output "My name is $name and I am $age years old"
3.3 Conditionals and Loops
$number = Read-Host "Enter a number"
if ($number -gt 10) {
Write-Output "Number is greater than 10"
} else {
Write-Output "Number is 10 or less"
}
For loop:
for ($i=1; $i -le 5; $i++) {
Write-Output "Iteration $i"
}
While loop:
$count = 1
while ($count -le 5) {
Write-Output $count
$count++
}
3.4 Functions
function Greet($name) {
Write-Output "Hello, $name"
}
Greet "Spud"
3
4. Advanced Bash
4.1 Arrays
arr=(apple banana cherry)
echo ${arr[1]} # banana
4.2 String Processing
text="Hello World"
echo ${text^^} # Uppercase
4.3 File Handling
echo "Hello" > [Link]
cat [Link]
4.4 Process Management
ps aux | grep bash
kill <PID>
4.5 Error Handling
trap 'echo "An error occurred."' ERR
false # triggers error
4.6 Scheduling with Cron
crontab -e
# Add: 0 5 * * * /path/to/[Link]
4
5. Advanced PowerShell
5.1 Working with Objects
Get-Process | Where-Object {$_.CPU -gt 100}
5.2 JSON, XML, CSV
$json = '{"name":"Spud"}' | ConvertFrom-Json
Write-Output $[Link]
5.3 Error Handling
try {
Get-Item [Link]
} catch {
Write-Output "File not found!"
}
5.4 Remote Management
Enter-PSSession -ComputerName Server01
6. Practical Projects
6.1 Backup Script (Bash)
#!/bin/bash
src="/home/user/Documents"
dest="/home/user/Backup"
tar -czf $dest/backup_$(date +%F).[Link] $src
6.2 System Info Script (PowerShell)
Get-ComputerInfo | Select-Object CsName, OsName, OsVersion
5
6.3 File Management
#!/bin/bash
mkdir -p ~/Documents/Sorted
mv ~/Downloads/*.pdf ~/Documents/Sorted/
7. Tips and Best Practices
• Always add comments
• Use meaningful variable names
• Test scripts in a safe environment
• Backup before running automation scripts
8. Appendices
Bash Cheat Sheet: - echo , read , if , for , while , function , trap
PowerShell Cheat Sheet: - Write-Output , Read-Host , if , for , while , function ,
try/catch
Resources: - Bash: [Link] - PowerShell: [Link]
en-us/powershell/
End of Guide