erlang 调用shell 命令

经常会有erlang程序调用外部程序的需求,比如调用shell命令程序,一般是用os:cmd/1,比如:

  1. 1> os:cmd("pwd").  
  2. "/home\n"  

不过os:cmd/1是不能获知外部程序的退出状态的,比如外部程序是正常退出还是异常退出。怎么获知外部程序的退出状态呢?可以用erlang:open_port/2,比如:
  1. 1> erlang:open_port({spawn, "erlc"}, [exit_status]).  
  2. #Port<0.573>  
  3. 2> flush().  
  4. Shell got {#Port<0.573>,{exit_status,0}}  
  5. ok  
  6. 3> erlang:open_port({spawn, "erlc a.erl"}, [exit_status]).  
  7. #Port<0.584>  
  8. 4> flush().                                                 
  9. Shell got {#Port<0.584>,{data,"a.erl:none: no such file or directory\n"}}  
  10. Shell got {#Port<0.584>,{exit_status,1}}  
  11. ok  

关于上面的exit_status: When the external process connected to the port exits, a message of the form {Port,{exit_status,Status}} is sent to the connected process, where Status is the exit status of the external process. If the program aborts, on Unix the same convention is used as the shells do (i.e., 128+signal).
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值