Exercise 08-01 — Shell Scripting: Basics¶
Navigation: ← Exercise 07 | Exercises Index | Next → Exercise 08-02 Note: 08-01 — Shell Scripting: Basics
Before You Start¶
Read Note 08-01 first.
Create a working directory for all your scripts:
Exercise 8.1 — Your First Script¶
Step 1: Create the script file:
Type this content:
#!/bin/bash
echo "Hello, World!"
echo "Today is: $(date)"
echo "I am logged in as: $(whoami)"
echo "My home directory is: $HOME"
Save and exit nano: press Ctrl + O, then Enter, then Ctrl + X.
Step 2: Try running it directly:
Expected error:
The file exists but is not executable yet. Every new file is created without execute permission — you must grant it explicitly.
Step 3: Make it executable:
Step 4: Run it again:
Expected output:
Hello, World!
Today is: Thu Jun 20 14:32:01 UTC 2024
I am logged in as: jahid
My home directory is: /home/jahid
Understand the script:
- #!/bin/bash — the shebang; tells Linux to run this file with bash
- $(date) — runs the date command and inserts its output here
- $(whoami) — runs whoami and inserts the result
- $HOME — a built-in variable that holds your home directory path
Reflect: What is the difference between $(whoami) and $HOME? One runs a command, the other reads a variable — can you tell which is which?
Exercise 8.2 — Variables and Arguments¶
Step 1: Create a script that accepts arguments:
Content:
#!/bin/bash
# Check that exactly one argument was provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <name>"
echo "Example: $0 Alice"
exit 1
fi
NAME=$1
echo "Hello, $NAME!"
echo "You were greeted at: $(date)"
Step 2: Test it correctly:
Expected output:
Step 3: Test the error handling by providing no argument:
Expected output:
Usage: /home/jahid/linux-practice/scripts/greet.sh <name>
Example: /home/jahid/linux-practice/scripts/greet.sh Alice
Understand the script:
- $# — the number of arguments passed to the script
- -ne 1 — "not equal to 1"
- $0 — the script's own name (used to show the user exactly how to call it)
- $1 — the first argument
- exit 1 — quit with error code 1 (non-zero = something went wrong)
Step 4: Try passing multiple arguments (the script should reject this too):
Exercise 8.3 — Arithmetic¶
Step 1: Create a calculator script:
Content:
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <number1> <number2>"
exit 1
fi
A=$1
B=$2
echo "Numbers: $A and $B"
echo "Addition: $A + $B = $((A + B))"
echo "Subtraction: $A - $B = $((A - B))"
echo "Multiplication: $A x $B = $((A * B))"
# Avoid division by zero
if [ $B -eq 0 ]; then
echo "Division: cannot divide by zero"
else
echo "Division: $A / $B = $((A / B))"
echo "Remainder: $A % $B = $((A % B))"
fi
Step 2: Test it:
Expected output:
Numbers: 15 and 4
Addition: 15 + 4 = 19
Subtraction: 15 - 4 = 11
Multiplication: 15 x 4 = 60
Division: 15 / 4 = 3
Remainder: 15 % 4 = 3
Notice division gives 3 not 3.75 — bash only does integer math. The decimal is simply dropped (not rounded).
Step 3: Test the division-by-zero protection:
Expected output:
Numbers: 10 and 0
Addition: 10 + 0 = 10
Subtraction: 10 - 0 = 10
Multiplication: 10 x 0 = 0
Division: cannot divide by zero
Understand $(( )):
- $((A + B)) — evaluates the math expression and substitutes the result
- No $ needed inside (( )) for variables — A and B work directly
- You can also write it as $((A+B)) — spaces are optional inside
Challenge — Personal Info Script¶
Write a script ~/linux-practice/scripts/about-me.sh that:
- Uses at least 3 built-in variables (
$USER,$HOME,$HOSTNAME,$$, etc.) - Uses at least 1 command substitution (e.g.,
$(date),$(uptime),$(nproc)) - Accepts an optional argument: if one is given, use it as a greeting name; if none, use
$USER
Example run with no argument:
Example run with argument ./about-me.sh Alice:
Navigation: ← Exercise 07 | Exercises Index | Next → Exercise 08-02 Note: 08-01 — Shell Scripting: Basics