Run shell commands in Elixir

后端 未结 6 741
醉酒成梦
醉酒成梦 2020-12-14 05:23

I want to execute a program through my Elixir code. What is the method to call a shell command to a given string? Is there anything which isn\'t platform specific?

相关标签:
6条回答
  • 2020-12-14 05:45

    I cannot link directly to the relevant documentation but it's here under the System module

    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.
    
    0 讨论(0)
  • 2020-12-14 05:47

    Here is how you execute a simple shell command without arguments:

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

    Checkout the documentation about System for more information.

    0 讨论(0)
  • 2020-12-14 05:59

    System.cmd/3 seems to accept the arguments to the command as a list and is not happy when you try to sneak in arguments in the command name. For example

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

    What in fact happens underneath is System.cmd/3 calls :os.find_executable/1 with its first argument, which works just fine for something like ls but returns false for ls -al for example.

    The erlang call expects a char list instead of a binary, so you need something like the following:

    "find /tmp -type f -size -200M |xargs rm -f" |> String.to_char_list |> :os.cmd
    
    0 讨论(0)
  • 2020-12-14 06:05

    The "devinus/sh" library is another interesting approach to run shell commands.

    https://github.com/devinus/sh

    0 讨论(0)
  • 2020-12-14 06:05

    If I have the following c program in the file a.c:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int arc, char** argv)
    {
    
        printf("%s\n",argv[1]);
        printf("%s\n",argv[2]);
    
        int num1 = atoi(argv[1]);
        int num2 = atoi(argv[2]);
    
        return num1*num2;
    }
    

    and compile the program to the file a:

    ~/c_programs$ gcc a.c -o a
    

    then I can do:

    ~/c_programs$ ./a 3 5
    3
    5
    

    I can get the return value of main() like this:

    ~/c_programs$ echo $?
    15
    

    Similarly, in iex I can do this:

    iex(2)> {output, status} = System.cmd("./a", ["3", "5"])
    {"3\n5\n", 15}
    
    iex(3)> status
    15
    

    The second element of the tuple returned by System.cmd() is the return value of the main() function.

    0 讨论(0)
  • 2020-12-14 06:12

    You can have a look in the Erlang os Module. E.g. cmd(Command) -> string() should be what you are looking for.

    0 讨论(0)
提交回复
热议问题