RacketSystemShellProcessScripting
Systems Programming: Shells and Processes | Schema Programming Part 28
2.485 min read
Md Nasim Sheikh
Code on GitHub: All examples from this tutorial are available at racket-programming-series/28-systems-programming — clone the repo and follow along!
Racket is an excellent alternative to Bash or Python for gling system tasks together.
Advertisement
Running Commands (system)
The simplest way to run a command:
(system "ls -F") ; Returns #t if successful
Capturing Output (process)
To get the output of a command into Racket:
(match-define (list stdout stdin pid stderr control)
(process "ls"))
(display (port->string stdout))
(close-input-port stdout)
(close-output-port stdin)
(close-input-port stderr)
Paths and Filesystem
Racket handles cross-platform paths gracefully.
(build-path "usr" "local" "bin")
; On Windows: "usr\local\bin"
; On Unix: "usr/local/bin"
(file-exists? "config.json")
(directory-list)
Rash: Racket as a Shell
There is even a language #lang rash that mixes Racket and Bash syntax!
#lang rash
ls -a | grep ".rkt"
Advertisement
Summary
Racket scripts are robust, fast, and cross-platform. You can use Racket to automate builds, manage files, or orchestration tools.
Quick Quiz
Which function handles OS-specific path separators automatically?
Written by
Md Nasim Sheikh
Software Developer at softexForge
Verified Author150+ Projects
Published: