Shell Scripting Basics
Casting single commands (ls, cp, chmod) is powerful, but repetitive. In Linux, true mastery lies in automation - binding commands into shell scripts that execute sequences automatically. Shell scripting is the foundation of system administration, DevOps, and automation, enabling consistency, efficiency, and integration across environments.
What is a Shell Script?
A shell script is a file containing a series of commands executed by the shell (commonly Bash).
- Automates repetitive tasks, Reduces human error.
- Provides structured logic with variables, conditions, loops, and functions.
Anatomy of Shell Scripts
A basic script looks like this:
#!/bin/bash
# Display a greeting message
echo "Welcome to Hackers kingdom!"
# Print system date
date
| # | Command / Symbol | Description |
|---|---|---|
| 1 | #!/bin/bash |
The shebang. It tells the system which shell should interpret the script. |
| 2 | echo |
Prints text written in double quotes. |
| 3 | date |
Displays current date/time. |
| 4 | # |
Lines starting with # explain code, ignored by the shell. |
Variables: Magical Ingredients
- A variable is a named storage location that holds data (like text, numbers, or paths). Think of them as containers that store information for your script to use later.
- In shell scripts, variables let you reuse values, pass information, and make scripts dynamic.
#!/bin/bash
name="Harry Potter"
echo "Welcome, $name!"
$nameโ Expands to the stored value inname.- Variables can hold strings, numbers, or command outputs.
PATH, HOME) are global magical constants. Local variables are defined inside scripts.Control Structures: Ritual Logic
Control structures are logical constructs in shell scripting that decide how and when commands run. They allow scripts to branch, repeat, or choose actions based on conditions.
Conditional Statements
- if-else: Executes commands based on whether a condition is true or false
if [ -f "file.txt" ]; then
echo "File exists"
else
echo "File not found"
fi
- elif: Adds multiple conditions
- case: A cleaner way to handle multiple choices
case $1 in
start) echo "Starting service" ;;
stop) echo "Stopping service" ;;
*) echo "Unknown option" ;;
esac
Looping Structures
- for loop: Repeats commands for each item in a list
for file in *.txt
do
echo "Processing $file"
done
- while loop: Runs as long as a condition is true
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count+1))
done
- until loop: Runs until a condition becomes true
until [ $count -gt 5 ]
do
echo "Count: $count"
count=$((count+1))
done
Jump Statements
- break: Exits a loop early
- continue: Skips the current iteration and moves to the next
Why Shell Scripting Rocks
| Benefit | Description |
|---|---|
| Automation | Run backups, deployments, or monitoring tasks without manual effort |
| Consistency | Scripts ensure tasks are done the same way every time |
| Efficiency | Saves time by chaining commands and reducing repetitive work |
| Integration | Works seamlessly with other Linux tools and programming languages |
Practical Exercises
#!/bin/bash
astronaut="Captain Star"
mission="Explore the Galaxy"
log="/tmp/space_log.txt"
echo "Welcome, $astronaut!"
echo "Mission: $mission"
# Create mission log
echo "Mission started by $astronaut: $mission" > $log
# Set permissions
chmod 644 $log
# Check log
if [ -f "$log" ]; then
echo "โ
Mission log ready!"
fi
# Loop
count=1
while [ $count -le 5 ]
do
echo "Star check $count: Twinkle โจ"
count=$((count+1))
done
# Function
blastoff() {
echo "Blastoff! ๐ $1 is flying into space!"
}
blastoff "$astronaut"Hackers Quest - Mini Project
Create a script that:
- Greets the user.
- Displays the date.
- Lists files in a directory.
- Backs up one file.
- Make it executable and schedule it daily.
Document each command as part of the โritual scroll.โ
Hackers Notebook
Single commands are powerful, but scripts are ceremonies. They bind your spells into repeatable incantations, transforming Linux into a system that works for you automatically. With scripting, you command not just one spell, but entire rituals of automation.
