Lcl · Core · Packages · Libraries


Sh

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"

namespace Sh

proc Sh::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

proc Sh::run! cmd (opts #{})

run command, return full result dict Does not throw on error

proc Sh::lines cmd

run command and return stdout as list of lines

proc Sh::ok? cmd

check if command succeeds (exit 0)

proc Sh::status cmd

get exit status of command

proc Sh::pipe cmdline

run shell pipeline cmdline is a string that will be passed to /bin/sh -c

proc Sh::pipe! cmdline

run shell pipeline, return full result

proc Sh::capture cmd

capture both stdout and stderr Returns #{stdout “…” stderr “…” status N}

proc Sh::silent cmd

srun command, discard output, return status

proc Sh::which name

find path to executable (or empty string if not found)

proc Sh::exists? name

check if executable exists in PATH

proc Sh::read path

read file contents

proc Sh::write path content

write content to file

proc Sh::append path content

append content to file

proc Sh::glob pattern

expand glob pattern

proc Sh::pwd

get current working directory

proc Sh::hostname

get hostname

proc Sh::whoami

get current user

proc Sh::date (fmt {})

get current date/time If fmt is empty or not provided, returns default date format

proc Sh::sleep seconds

sleep for specified seconds

proc Sh::mkdir path

create directory (with parents)

proc Sh::rm path

remove file or directory

proc Sh::cp src dst

copy file or directory

proc Sh::mv src dst

move/rename file or directory

proc Sh::file? path

check if path is a regular file

proc Sh::dir? path

check if path is a directory

proc Sh::readable? path

check if path is readable

proc Sh::writable? path

check if path is writable

proc Sh::executable? path

check if path is executable

proc Sh::env vars cmd

run command with environment variables Convenience wrapper that builds opts dict

proc Sh::in dir cmd

run command in specified directory Convenience wrapper that builds opts dict

proc Sh::stdin input cmd

run command with stdin input Convenience wrapper that builds opts dict