Skip to main content
Back to contributions
Pull Request
Merged
4.5K

Add system utility stdlib functions

amber-lang/amber

Added 24 new stdlib functions for system utilities including uname, wc, sort, uniq, mount/umount, and pgrep/pkill

The Problem

Amber is a modern programming language that compiles to Bash, designed to make shell scripting safer and more maintainable. The stdlib was missing common system utility functions that developers needed for everyday tasks like getting system info, managing processes, and manipulating text.

Issue #951 requested these utilities to reduce the need for raw Bash fallbacks.

The Solution

I implemented 24 new stdlib functions across two modules, following Amber’s design principles of type safety and clean syntax.

System Information (src/std/env.ab)

// Get system details
let kernel = uname_kernel_name()    // "Linux"
let host = uname_nodename()         // "myserver"
let all = uname_all()               // Full uname output

Functions added:

  • uname_kernel_name(), uname_nodename(), uname_kernel_release(), uname_kernel_version(), uname_machine(), uname_os(), uname_all()

Process Management (src/std/env.ab)

// Find and kill processes
let pids = pgrep("nginx")           // Get PIDs matching pattern
pkill_exact("zombie-process")       // Kill exact match
kill(1234, "SIGTERM")              // Send signal to PID

Functions added:

  • pgrep(), pgrep_exact() - process discovery using native Amber loops
  • pkill(), pkill_exact(), pkill_force() - process termination
  • kill() - send arbitrary signals to PIDs

Filesystem Operations (src/std/env.ab)

// Mount with sudo modifier
sudo mount("/dev/sdb1", "/mnt/usb")
sudo umount("/mnt/usb")

Functions added:

  • mount(), umount(), umount_force() - with sudo modifier support

Text Processing (src/std/text.ab)

let text = "hello\nworld\nhello"
let count = lines_count(text)       // 3
let sorted = sort_lines(text)       // Alphabetically sorted
let unique = uniq_all_lines(text)   // Deduplicated

Functions added:

  • lines_count(), words_count(), chars_count()
  • sort_lines(), sort_lines_desc(), sort_lines_numeric()
  • uniq_lines(), uniq_all_lines()

Files Changed

FileChanges
src/std/env.ab+15 functions for system/process operations
src/std/text.ab+9 functions for text processing
src/tests/validity/*.ab17 test files with comprehensive coverage

Review Process

The PR went through multiple iterations based on maintainer feedback from @Ph0enixKM and @Tirito6626:

  • Native Amber over Bash: Converted while loops to native Amber loop constructs
  • Here-string syntax: Adopted cleaner syntax over printf pipes
  • v0.6.0 compatibility: Updated echo syntax to match new language version
  • Sudo modifier: Added proper sudo support for mount operations

Timeline

DateEvent
2025-12-18Implemented all functions and tests
2025-12-19Addressed review feedback, converted to native Amber patterns
2025-12-19Added kill() function, updated to v0.6.0 syntax
2025-12-21PR reworked and merged via #965