Lcl · Core · Packages · Libraries
sh.lcl - Shell scripting sugar layer
Provides convenient shell scripting functions built on Process::*
REQUIRES: lcl-process package (provides Process:: namespace)
Usage:
load lib/sh.lcl
;; Simple command execution (returns stdout, trimmed)
let files [Sh::run (ls -la)]
;; Get output as list of lines
let items [Sh::lines (ls)]
;; Check if command succeeds
if [Sh::ok? (which git)] {
puts "git is installed"
}
;; Run with options
Sh::run (./script.sh) #{env #{DEBUG 1} cwd /tmp}
;; Pipe commands (using shell)
Sh::pipe "ls -la | grep foo | wc -l"ShSh::run cmd (opts #{})Sh::run cmd ?opts? - run command and return stdout (trimmed) Options: throw - 1 (default) to throw on error, 0 to return empty string stdin - string to send to stdin env - dict of environment variables cwd - working directory
Sh::run! cmd (opts #{})run command, return full result dict Does not throw on error
Sh::lines cmdrun command and return stdout as list of lines
Sh::ok? cmdcheck if command succeeds (exit 0)
Sh::status cmdget exit status of command
Sh::pipe cmdlinerun shell pipeline cmdline is a string that will be passed to /bin/sh -c
Sh::pipe! cmdlinerun shell pipeline, return full result
Sh::capture cmdcapture both stdout and stderr Returns #{stdout “…” stderr “…” status N}
Sh::silent cmdsrun command, discard output, return status
Sh::which namefind path to executable (or empty string if not found)
Sh::exists? namecheck if executable exists in PATH
Sh::read pathread file contents
Sh::write path contentwrite content to file
Sh::append path contentappend content to file
Sh::glob patternexpand glob pattern
Sh::pwdget current working directory
Sh::hostnameget hostname
Sh::whoamiget current user
Sh::date (fmt {})get current date/time If fmt is empty or not provided, returns default date format
Sh::sleep secondssleep for specified seconds
Sh::mkdir pathcreate directory (with parents)
Sh::rm pathremove file or directory
Sh::cp src dstcopy file or directory
Sh::mv src dstmove/rename file or directory
Sh::file? pathcheck if path is a regular file
Sh::dir? pathcheck if path is a directory
Sh::readable? pathcheck if path is readable
Sh::writable? pathcheck if path is writable
Sh::executable? pathcheck if path is executable
Sh::env vars cmdrun command with environment variables Convenience wrapper that builds opts dict
Sh::in dir cmdrun command in specified directory Convenience wrapper that builds opts dict
Sh::stdin input cmdrun command with stdin input Convenience wrapper that builds opts dict