2017-09-13 77 views
-1

我想在.EX文件來執行這個命令 -如何在.ex文件中執行這個openssl命令?

openssl ec -in myprivatekey.pem -outform DER|tail -c +8|head -c 32|xxd -p -c 32 

,我已經翻譯成

{_, 0} = System.cmd "openssl", [ "ec", "-in", private_key_file, "-outform", "DER|tail", "-c", "+8|head", "-c", "32|xxd", "-p", "-c", "32"], [stderr_to_stdout: true] 
在靈藥

,但即時得到下面的錯誤 - error

如何正確執行這個openssl命令?

回答

2

你的第一個代碼片段實際上是執行多個命令(openssl,tail,head,xxd)和管道數據從一個到另一個。 System.cmd只產生一個命令,不會自動處理管道。

您可以使用:os.cmd/1來執行這一點,這將產生使用系統的默認shell命令,這應該處理管道:

# Note that this takes the command as a charlist and does not return the exit code 
output = :os.cmd('openssl ec -in myprivatekey.pem -outform DER|tail -c +8|head -c 32|xxd -p -c 32') 

另一種方法是將命令傳遞給自己使用System.cmd殼。以下應該適用於存在/bin/sh的系統:

{stdout, 0} = System.cmd("/bin/sh", ["-c", "openssl ec -in myprivatekey.pem -outform DER|tail -c +8|head -c 32|xxd -p -c 32"])