JanetDocsPlaygroundI'm feeling luckyGitHub sign in

os/execute



    cfunction
    src/core/os.c on line 1104, column 1

    (os/execute args &opt flags env)

    Execute a program on the system and pass it string arguments. 
    `flags` is a keyword that modifies how the program will execute.

    * :e - enables passing an environment to the program. Without :e, 
      the current environment is inherited.
    * :p - allows searching the current PATH for the binary to execute. 
      Without this flag, binaries must use absolute paths.
    * :x - raise error if exit code is non-zero.
    * :d - Don't try and terminate the process on garbage collection 
      (allow spawning zombies).

    `env` is a table or struct mapping environment variables to values. 
    It can also contain the keys :in, :out, and :err, which allow 
    redirecting stdio in the subprocess. These arguments should be 
    core/file values. Returns the exit status of the program.


3 examplesSign in to add an example
Loading...
(def [stdin-r stdin-w] (os/pipe))
(def [stdout-r stdout-w] (os/pipe))

# write the input that will be sent to sed
(:write stdin-w "hello world 1\nhello world 2")
(:close stdin-w)

(os/execute
  @("sed" "s|world|janet|g")
  :px
  # the program reads from :in and writes to :out
  {:in stdin-r :out stdout-w})

(:read stdout-r math/int32-max)
# => @"hello janet 1\nhello janet 2"

# feed two lines to sed, which replaces "world"
# with "janet", and read the modified results back
YohananDiamondPlayground
(os/execute
  @("/usr/bin/bash" "-c" "set")
  :e
  @{"SOME" "value"
    "OTHER" "one"})
# => 0

# execute bash and prints environment variables
# which contains SOME=value and Other=one
goldenHairDafoPlayground
(os/execute
  @("python" "-c" "print('Hello Janet'"))
  :p) 
# => 0

# execute python -c "print('Hello Janet') while
# searching path on the current path
goldenHairDafoPlayground