๐ง Device & Peripheral I/O
Shell scripts frequently interact with system devices such as printers, USB devices, serial ports, disks, audio interfaces, and network adapters. This module provides a practical overview of how POSIX shells integrate with system peripherals using standard Linux interfaces.
๐ Who This Is For
- DevOps/SRE engineers managing Linux servers or workstations
- Developers writing automation for hardwareโrelated tasks
- Sysadmins working with printers, USB devices, or serial ports
- Anyone needing predictable, scriptable access to system peripherals
๐งฉ Role in the Ecosystem
Linux exposes hardware through:
/dev/*device nodes- sysfs (
/sys/class/*) - udev events and rules
- system services (CUPS, NetworkManager, PulseAudio/PipeWire)
- commandโline utilities (lp, lsusb, ip, amixer, scanimage, mount)
Shell scripts act as glue between these interfaces and higherโlevel automation.
๐งฉ Key Concepts
1. Device Nodes
Hardware is represented as files:
/dev/sdaโ block devices/dev/ttyUSB0โ serial adapters/dev/lp0โ parallel printers/dev/input/*โ keyboards, mice
2. sysfs
Provides structured metadata:
1 2 | |
3. udev
Dynamic device management:
1 | |
4. Permissions
Most device nodes require group membership (lp, dialout, audio, video).
๐ง Techniques
Printers (CUPS)
List printers:
1 | |
Print a file:
1 | |
Check queue:
1 | |
Cancel job:
1 | |
USB Devices
List USB devices:
1 | |
Monitor hotplug events:
1 | |
Get detailed info:
1 | |
Serial Ports (TTY)
Configure serial port:
1 | |
Read data:
1 | |
Write data:
1 | |
Interactive session:
1 | |
Disks & Storage
List block devices:
1 | |
Identify filesystem:
1 | |
Mount:
1 | |
Unmount:
1 | |
Audio Devices
List sinks/sources:
1 | |
Set volume:
1 | |
Record audio:
1 | |
Network Interfaces
List interfaces:
1 | |
Bring interface up/down:
1 2 | |
Check WiโFi:
1 | |
Scanners (SANE)
List scanners:
1 | |
Scan to file:
1 | |
โ ๏ธ Limitations & Pitfalls
- Device access often requires elevated permissions
- Hotplug timing issues can break scripts
- USB device names (
/dev/sdX,/dev/ttyUSB*) are not stable - CUPS may require authentication
- Audio backends differ (PulseAudio vs PipeWire vs ALSA)
- Serial communication requires correct baud/parity settings
- sysfs paths vary across kernel versions
๐ง When to Use Shell for Device I/O
- Simple automation tasks
- Monitoring device state
- Integrating with existing CLI tools
- Lightweight scripts for servers or embedded systems
- Quick diagnostics and troubleshooting
โ When Not to Use Shell
- Highโperformance data acquisition
- Complex binary protocols
- Realโtime communication
- Applications requiring robust error recovery
- Multiโthreaded or asynchronous I/O
Use a real language (Python, Go, Rust) for these cases.
โ Best Practices
- Use stable identifiers (UUIDs, labels, udev rules)
- Avoid hardโcoding
/dev/sdXpaths - Validate device presence before use
- Use
udevadm settlefor hotplug timing - Log all device interactions
- Handle permissions gracefully
- Use traps for cleanup:
1 | |
๐งช Testing Device I/O
1 2 3 4 5 6 | |
๐ง Summary
Shell scripts provide a powerful interface to Linux peripherals through /dev, sysfs, udev, and system utilities.
They are ideal for lightweight automation, diagnostics, and integration โ but not for highโperformance or realโtime workloads.