Elixir change to a directory and run a shell script

Viewed 233

In elixir code I am looking to change location to a directory and run a script there

File.cd!("/opt/test/", System.cmd("./bin/myscript.sh", []))

The second argument to File.cd! is a function and I thought the System.cmd would satisfy the requirement of being a function but the editor I am using says this statement will never work.

Is there some easier way to change to a directory, and execute a shell script?

3 Answers

According to docs of System.cmd/3 there is option :cd. So you need to use just that:

System.cmd("./bin/myscript.sh", [], cd: "/opt/test/")

How about

System.cmd("/opt/test/bin/myscript.sh", [])

If you want to pass a function, you need to make it a lambda explicitly.

File.cd!("/opt/test/", fn() -> System.cmd("./bin/myscript.sh", []))
Related