Przejdลบ do treล›ci

๐Ÿง  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
ls /sys/class/net
ls /sys/class/usb_device

3. udev

Dynamic device management:

1
udevadm info /dev/sda

4. Permissions

Most device nodes require group membership (lp, dialout, audio, video).


๐Ÿ”ง Techniques

Printers (CUPS)

List printers:

1
lpstat -p

Print a file:

1
lp -d printer_name file.pdf

Check queue:

1
lpq

Cancel job:

1
cancel JOB_ID

USB Devices

List USB devices:

1
lsusb

Monitor hotplug events:

1
udevadm monitor --udev

Get detailed info:

1
udevadm info --query=all --name=/dev/sdb

Serial Ports (TTY)

Configure serial port:

1
stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb

Read data:

1
cat /dev/ttyUSB0

Write data:

1
echo "AT" > /dev/ttyUSB0

Interactive session:

1
screen /dev/ttyUSB0 9600

Disks & Storage

List block devices:

1
lsblk

Identify filesystem:

1
blkid /dev/sdb1

Mount:

1
mount /dev/sdb1 /mnt

Unmount:

1
umount /mnt

Audio Devices

List sinks/sources:

1
pactl list short sinks

Set volume:

1
pactl set-sink-volume @DEFAULT_SINK@ +5%

Record audio:

1
arecord -d 5 out.wav

Network Interfaces

List interfaces:

1
ip link

Bring interface up/down:

1
2
ip link set eth0 up
ip link set eth0 down

Check Wiโ€‘Fi:

1
iw dev

Scanners (SANE)

List scanners:

1
scanimage -L

Scan to file:

1
scanimage --format=png > out.png

โš ๏ธ 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/sdX paths
  • Validate device presence before use
  • Use udevadm settle for hotplug timing
  • Log all device interactions
  • Handle permissions gracefully
  • Use traps for cleanup:
1
trap 'umount /mnt' EXIT

๐Ÿงช Testing Device I/O

1
2
3
4
5
6
lsblk
lsusb
udevadm monitor
echo test > /dev/ttyUSB0
scanimage -L
ip link

๐Ÿง  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.