JanetDocsSourcePlaygroundI'm feeling luckyCommunityGitHub sign in

os/execute

core-api


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

    (os/execute args &opt flags env)

    Execute a program on the system and return the exit code. `args` is 
    an array/tuple of strings. The first string is the name of the 
    program and the remainder are arguments passed to the program. 
    `flags` is a keyword made from the following characters that 
    modifies how the program executes:

    * :e - enables passing an environment to the program. Without 'e', 
      the current environment is inherited.
    * :p - allows searching the current PATH for the program to 
      execute. Without this flag, the first element of `args` must be 
      an absolute path.
    * :x - raises error if exit code is non-zero.
    * :d - prevents the garbage collector terminating the program (if 
      still running) and calling the equivalent of `os/proc-wait` 
      (allows zombie processes).

    `env` is a table/struct mapping environment variables to values. It 
    can also contain the keys :in, :out, and :err, which allow 
    redirecting stdio in the subprocess. :in, :out, and :err should be 
    core/file or core/stream values. If core/stream values are used, 
    the caller is responsible for ensuring pipes do not cause the 
    program to block and deadlock.


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