program story

Elixir에서 셸 명령 실행

inputbox 2020. 11. 24. 07:58
반응형

Elixir에서 셸 명령 실행


Elixir 코드를 통해 프로그램을 실행하고 싶습니다. 주어진 문자열에 대해 쉘 명령을 호출하는 방법은 무엇입니까? 특정 플랫폼이 아닌 것이 있습니까?


인수없이 간단한 쉘 명령을 실행하는 방법은 다음과 같습니다.

System.cmd("whoami", [])
# => {"lukas\n", 0}

System자세한 내용 은 문서 를 확인하세요.


Erlang os 모듈을 살펴볼 수 있습니다 . 예를 들어 cmd(Command) -> string()당신이 찾고있는 것이어야합니다.


"devinus / sh"라이브러리는 쉘 명령을 실행하는 또 다른 흥미로운 접근 방식입니다.

https://github.com/devinus/sh


System.cmd / 3는 명령에 대한 인수를 목록으로 받아들이는 것처럼 보이며 명령 이름에 인수를 몰래 넣으려고 할 때 만족하지 않습니다. 예를 들면

System.cmd("ls", ["-al"]) #works, while
System.cmd("ls -al", []) #does not.

실제로 아래에서 일어나는 일은 System.cmd / 3가 첫 번째 인수로 : os.find_executable / 1을 호출하는 것입니다. 이것은 ls와 같은 경우에는 잘 작동하지만 ls -al에 대해서는 false를 반환합니다.

erlang 호출은 바이너리 대신 char 목록을 기대하므로 다음과 같은 것이 필요합니다.

"find /tmp -type f -size -200M |xargs rm -f" |> String.to_char_list |> :os.cmd

나는 관련 문서에 직접 링크 할 수 없습니다하지만 그건 여기 세 이하 System모듈

cmd(command) (function) # 
Specs:

cmd(char_list) :: char_list
cmd(binary) :: binary
Execute a system command.

Executes command in a command shell of the target OS, captures the standard output of the command and returns the result as a binary.

If command is a char list, a char list is returned. Returns a binary otherwise.

참고 URL : https://stackoverflow.com/questions/22594988/run-shell-commands-in-elixir

반응형