Os.which
func Os.which(program: string): string | nullOs.which returns the path of the executable in PATH that a Cmd naming program starts, or null when PATH holds none. A program with a path separator is returned unchanged when it names an executable file, taken against the working directory, and null otherwise.
Details
Os.whichlooksprogramup in the program's ownPATH,Os.env("PATH"), as a Cmd does. Theenvof aCmdnever changes that lookup. An entry ofPATHthat is not absolute,.included, finds nothing.- A
programwith a path separator is not looked up. It comes back unchanged when it names an executable file, taken against the working directory, andnullotherwise. ACmdwithdirtakes it againstdirinstead, whichOs.whichcannot be given.
Basic examples
Find a program in PATH:
println(Os.which("sh"))A program that no directory of PATH holds gives null:
println(Os.which("no-such-program"))nullScope
The result is string | null. Narrow it before using it as a path; the path is absolute and ends with the program's name:
let sh = Os.which("sh")
if sh != null {
println(Path.isAbs(sh))
println(Path.base(sh))
}true
shGive a default with ?? to fall back to the bare name:
let shell = Os.which("no-such-shell") ?? "sh"
println(shell)shAn empty name, or one with a trailing space, finds nothing:
println(Os.which(""))
println(Os.which("sh "))null
nullA name with a path separator is not looked up in PATH:
println(Os.which("/bin/sh"))
println(Os.which("./no-such-tool"))/bin/sh
nullApplications
Fail early, with a message that says what to install, when a script needs a tool:
func requireTool(name: string): Result[string, Error] {
let path = Os.which(name) ?? return Result.fail("${name} not found in PATH; install it first")
Result.ok(path)
}
println(requireTool("no-such-tool"))
println(requireTool("sh").isOk())Err{ error: Error{ message: "no-such-tool not found in PATH; install it first" } }
truePick the first program of several that is installed, such as a pager:
let pager = ["no-such-pager", "cat"].find({ Os.which(x) != null }) ?? "more"
println(pager)catFall back to Ant code when a tool is missing:
func countLines(text: string): Result[string, Error] {
if Os.which("wc") == null { return Result.ok("${text.split("\n").length()}") }
let count = try Cmd.read(Cmd.new("wc", ["-l"]), input: "${text}\n")
Result.ok(count.trim())
}
println(try countLines("a\nb\nc"))3Properties and relations
null from Os.which means a Cmd naming the program fails to start:
let missing = "no-such-program"
assert(Os.which(missing) == null)
assert(!Cmd.run(Cmd.new(missing)).isOk())A Cmd naming a program that Os.which finds starts it:
assert(Os.which("sh") != null)
let out = try Cmd.read(Cmd.new("sh", ["-c", "echo ok"]))
assert(out == "ok")Possible issues
Setting PATH in a command's env does not change where the command's program is found. The program's own PATH still finds sh here, and Os.which agrees with it:
let c = Cmd.new("sh", ["-c", "echo ok"], env: Map{ "PATH": "/no-such-dir" })
println(Cmd.read(c))
println(Os.which("sh") != null)Ok{ value: "ok" }
trueSee also
Cmd.new · Cmd.run · Cmd.read · Cmd.start · Os.env · Path.base
