LEVEL 1

API & SHELL SCRIPTING
FUNDAMENTALS

Learning the Building Blocks

IMPORTANT: This workshop uses different examples than your project. You'll need to apply these concepts to build your weather dashboard.
TIME
90 MINS
FORMAT
LIVE CODE
GOAL
FOUNDATIONS

WORKSHOP AGENDA

SETUP: GETTING STARTED

1. OPEN YOUR TERMINAL

  • Mac: Press Cmd + Space, type "Terminal", press Enter
  • Linux: Press Ctrl + Alt + T or search for "Terminal"
  • Windows: Search for "Git Bash" in Start menu

2. CREATE WORKSHOP FOLDER

terminal
# Make a new directory for our workshop
mkdir api-workshop
cd api-workshop

# Verify you're in the right place
pwd
# Should show: /home/yourusername/api-workshop
01

MAKING API REQUESTS (JOKES API)

We'll use a free jokes API to learn HTTP requests. Same concepts as weather, but different data format!

DEMO 1: YOUR FIRST API REQUEST

Step 1: Create Script
touch joke_fetcher.sh
chmod +x joke_fetcher.sh
nano joke_fetcher.sh
Step 2: Write Code
joke_fetcher.sh
#!/bin/bash

echo "=== Random Joke Fetcher ==="
echo ""

# Fetch a random joke from the API
curl -s "https://official-joke-api.appspot.com/random_joke"
Step 3: Run It!

Run: ./joke_fetcher.sh

Output should be raw JSON data.

Understanding curl:
  • curl: Tool for HTTP requests
  • -s: Silent mode (hides progress bar)

DEMO 2: PARSING THE RESPONSE

Create parse_joke.sh:

parse_joke.sh
#!/bin/bash

echo "Fetching a random joke..."
echo ""

# Store the response in a variable
response=$(curl -s "https://official-joke-api.appspot.com/random_joke")

# Check if we got data
if [ -z "$response" ]; then
    echo "Error: No data received!"
    exit 1
fi

# Extract fields
setup=$(echo "$response" | grep -o '"setup":"[^"]*"' | cut -d'"' -f4)
punchline=$(echo "$response" | grep -o '"punchline":"[^"]*"' | cut -d'"' -f4)

# Display nicely
echo "=== Random Joke ==="
echo "Q: $setup"
echo "A: $punchline"
echo "==================="
Breaking Down Parsing:
grep -o '"setup":"[^"]*"' | cut -d'"' -f4

Finds the field pattern, then cuts the string by quotes to get the value.

DEMO 3: USER INPUT & LOOPS

Create joke_menu.sh:

joke_menu.sh
#!/bin/bash

# Ask user input
read -p "How many jokes? (1-5): " count

# Validate: Empty? Numeric? Range?
if [ -z "$count" ] || ! [[ "$count" =~ ^[0-9]+$ ]] || [ "$count" -lt 1 ] || [ "$count" -gt 5 ]; then
    echo "Error: Enter a number between 1-5"
    exit 1
fi

echo "Fetching $count jokes..."

for i in $(seq 1 $count); do
    response=$(curl -s "https://official-joke-api.appspot.com/random_joke")
    setup=$(echo "$response" | grep -o '"setup":"[^"]*"' | cut -d'"' -f4)
    punchline=$(echo "$response" | grep -o '"punchline":"[^"]*"' | cut -d'"' -f4)
    
    echo ""
    echo "Joke #$i:"
    echo "Q: $setup"
    echo "A: $punchline"
done

KEY CONCEPTS LEARNED:

  • ✅ Storing API response: var=$(curl...)
  • ✅ Checking empty data: [ -z "$var" ]
  • ✅ Parsing JSON with grep/cut
  • ✅ Input validation
  • ✅ Loops: for i in $(seq 1 $n)

PRACTICE EXERCISE 1

The jokes API has a "type" field. Modify parse_joke.sh to display the joke type as well.

02

GETTING SYSTEM INFORMATION

Exploring system commands you might not use directly, but that teach the concepts.

DEMO 4: RESOURCE INFO

Create resource_info.sh:

resource_info.sh
#!/bin/bash

echo "=== RESOURCE INFO ==="

echo "Shell: $SHELL"
echo "OS: $(uname -s)"
echo "Kernel: $(uname -r)"

echo "CPU Cores:"
nproc 2>/dev/null || echo "Unknown"

# Memory (Linux only)
if [ -f /proc/meminfo ]; then
    grep MemTotal /proc/meminfo
fi

echo ""
echo "Top 5 Processes by Memory:"
ps aux --sort=-%mem | head -6

DEMO 5: FILE EXPLORER

Create file_explorer.sh:

file_explorer.sh
#!/bin/bash

echo "Current Dir: $(pwd)"

echo "Stats:"
# Count lines with wc -l
echo "Total Items: $(ls -1 | wc -l)"
echo "Directories: $(ls -l | grep ^d | wc -l)"
echo "Files: $(ls -l | grep ^- | wc -l)"

echo ""
echo "5 Largest Files:"
# awk prints specific columns (size and name)
ls -lhS | grep ^- | head -5 | awk '{print $5, $9}'

PRACTICE EXERCISE 2

Create my_info.sh that displays: username, home dir, directory size, and a message if >10 files exist.

03

INTERACTIVE MENUS (CALCULATOR)

DEMO 6: BASIC MENU

Create calculator.sh:

calculator.sh
#!/bin/bash

while true; do
    echo "1. Add"
    echo "2. Exit"
    read -p "Choice: " choice
    
    case $choice in
        1)
            read -p "Num 1: " n1
            read -p "Num 2: " n2
            echo "Sum: $((n1 + n2))"
            ;;
        2)
            echo "Bye!"
            exit 0
            ;;
        *)
            echo "Invalid!"
            ;;
    esac
    echo ""
done

DEMO 7: FUNCTIONS

Create calc_v2.sh to organize code better:

calc_v2.sh
#!/bin/bash

# Define Function
do_add() {
    read -p "N1: " n1
    read -p "N2: " n2
    echo "Result: $((n1 + n2))"
}

while true; do
    echo "1. Add"
    echo "2. Exit"
    read -p "Choice: " c
    
    case $c in
        1) do_add ;;
        2) exit 0 ;;
        *) echo "Invalid" ;;
    esac
done

DEMO 8: ERROR HANDLING

# Check exit code ($?) of last command

curl -s "https://api.site.com"

if [ $? -eq 0 ]; then

  echo "Success!"

else

  echo "Failed!"

fi

04

MINI PROJECT: FACT FETCHER

THE MISSION

Create a script that fetches random facts from:
https://uselessfacts.jsph.pl/random.json?language=en

  • > Create menu: 1. Random Fact, 2. Get 3 Facts, 3. Exit
  • > Parse the "text" JSON field
  • > Loop until user exits
Starter Template
#!/bin/bash

get_fact() {
   # TODO: Fetch, Check Error, Parse
}

while true; do
   # TODO: Menu Loop & Case
done

SOLUTION CODE (Try it yourself first!)

#!/bin/bash

get_fact() {
    response=$(curl -s "https://uselessfacts.jsph.pl/random.json?language=en")
    if [ -z "$response" ]; then
        echo "Error fetching fact!"
        return
    fi
    fact=$(echo "$response" | grep -o '"text":"[^"]*"' | cut -d'"' -f4)
    echo "📚 Fact: $fact"
}

while true; do
    echo "1. Fact  2. Three Facts  3. Exit"
    read -p "Choice: " c
    case $c in
        1) get_fact ;;
        2) for i in 1 2 3; do get_fact; done ;;
        3) exit 0 ;;
        *) echo "Invalid" ;;
    esac
    echo ""
done

HOW THIS APPLIES TO YOUR PROJECT

WORKSHOP EXAMPLE YOUR WEATHER PROJECT
Jokes APIWeather API (wttr.in)
Parse "setup"/"punchline"Parse "temp_C"/"humidity"
Calculator MenuWeather Dashboard Menu
Get number inputGet city name input

ADAPT THESE PATTERNS, DON'T COPY THEM!

ESSENTIAL PATTERNS REFERENCE

# API REQUEST
var=$(curl -s "URL")
# INPUT VALIDATION
if [ -z "$input" ]; then exit 1; fi
# MENU LOOP
while true; do ... done
# FUNCTIONS
my_func() { ... }

COMMON MISTAKES TO AVOID

  • ❌ Not making scripts executable (chmod +x script.sh)
  • ❌ Using single quotes for variables ('$var' won't expand, use "$var")
  • ❌ Forgetting the shebang (#!/bin/bash)
  • ❌ Not checking if API response is empty

ORGANIZING YOUR FILES

mkdir reference-scripts
cp *.sh reference-scripts/
echo "Backup complete!"

GETTING HELP

Stuck? 1. Review scripts. 2. Check error messages. 3. Use echo to debug. 4. Beep me on Slack!

Don't ask "Can you write my script?". Ask "How do I parse this JSON?".

MISSION COMPLETE!