🧠 5. Raspberry Pi – Programming & Scripting
Estimated reading: 3 minutes 25 views

πŸ–₯️ Raspberry Pi – Shell Scripting Basics (2025 Beginner’s Automation Guide)


🧲 Introduction – Automate Tasks with Shell Scripts on Raspberry Pi

Shell scripting allows you to automate repetitive tasks, control hardware, manage files, or monitor your Raspberry Piβ€”all through the command line. It’s a vital skill for Raspberry Pi users building IoT, Linux automation, or embedded projects.

🎯 In this guide, you’ll learn:

  • How to write, run, and schedule shell scripts on Raspberry Pi
  • Bash syntax essentials: variables, loops, conditionals
  • Real-world Raspberry Pi automation examples
  • Best practices for script organization and debugging

πŸ“‚ What Is a Shell Script?

A shell script is a text file containing a series of shell (Bash) commands that the Raspberry Pi executes sequentially.

βœ… Create a script:

nano myscript.sh

βœ… Add content:

#!/bin/bash
echo "Hello, Raspberry Pi!"

βœ… Make it executable:

chmod +x myscript.sh

βœ… Run the script:

./myscript.sh

Output:

Hello, Raspberry Pi!

🧱 Basic Syntax and Constructs

πŸ“Œ Variables:

name="Raspberry"
echo "Hello $name Pi!"

πŸ“Œ Conditionals:

if [ $temp -gt 40 ]; then
  echo "Overheating!"
fi

πŸ“Œ Loops:

for i in {1..5}
do
  echo "Blink $i"
done

πŸ“Œ User Input:

read -p "Enter your name: " user
echo "Welcome $user"

πŸ”Œ Example: LED Control with Shell Script + GPIO

βœ… Script: led_on.sh

#!/bin/bash
gpio -g mode 17 out
gpio -g write 17 1
echo "LED ON"

βœ… Script: led_off.sh

#!/bin/bash
gpio -g write 17 0
echo "LED OFF"

βœ… These control an LED connected to GPIO17 using the gpio CLI tool (install with sudo apt install wiringpi or gpio-utils depending on OS version).


πŸ” Example: System Monitor Script

βœ… Script: sysinfo.sh

#!/bin/bash
echo "CPU Temp:"
vcgencmd measure_temp

echo "Disk Usage:"
df -h

echo "Memory Usage:"
free -h

βœ… Automates hardware health check for Pi.


⏲️ Automate with Cron (Task Scheduler)

βœ… Edit crontab:

crontab -e

βœ… Run a script every hour:

0 * * * * /home/pi/scripts/sysinfo.sh >> /home/pi/logs/status.log

βœ… Great for logging data or automating GPIO controls.


🧠 Tips for Writing Effective Shell Scripts

βœ… Best Practice⚠️ Avoid
Use #!/bin/bash at the topOmitting shebang (won’t execute)
Test commands individuallyWriting without checking output
Add comments (#)Unclear logic and flow
Use meaningful variable namesUsing generic names like x, y
Always handle permissionsRunning scripts blindly as root

πŸ”§ Real-World Raspberry Pi Shell Script Projects

πŸ› οΈ Project NameπŸ” Description
Auto Wi-Fi ReconnectScript to reconnect dropped Wi-Fi signal
Camera TimelapseCron + libcamera to capture every minute
LED Blinking AutomationShell loop with gpio commands
Safe Shutdown ButtonGPIO input triggers shutdown via script
Pi Self-Healing ScriptMonitor and restart failed processes

πŸ“Œ Summary – Recap & Next Steps

Shell scripting is a core tool for controlling, automating, and managing your Raspberry Pi through Linux. It enables fast prototyping and hands-free control of hardware and system services.

πŸ” Key takeaways:

  • Shell scripts are sequences of Bash commands with .sh extensions
  • You can automate hardware (GPIO), system checks, and user interactions
  • Cron allows scheduling scripts periodically
  • Shell scripting is essential for headless Pi projects and automation tasks

βš™οΈ Real-world relevance: Used in headless servers, startup routines, camera control, IoT sensors, and custom command-line utilities.


❓ FAQs – Raspberry Pi Shell Scripting

❓ Do I need to install anything to use shell scripts?

βœ… No. Raspberry Pi OS comes with Bash and all shell scripting basics pre-installed.


❓ Can I use shell scripts to control GPIO?

βœ… Yes. Use the gpio command-line tool (wiringpi or gpio-utils) or call Python scripts from shell.


❓ How do I debug a shell script?

βœ… Add set -x at the top or run with:

bash -x script.sh

❓ Can I trigger a shell script on boot?

βœ… Yes. Add it to rc.local, ~/.bashrc, or a systemd service file.


❓ What is the best way to log script output?

βœ… Use redirection:

./myscript.sh >> log.txt 2>&1

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ–₯️ Raspberry Pi – Shell Scripting Basics

Or Copy Link

CONTENTS
Scroll to Top