Guest User

Untitled

a guest
Feb 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'open3'
  3.  
  4. # CONTEXT
  5. #
  6. # I need to print stdout message and raise(stderr) on error
  7. #
  8. # System used: OpenBSD
  9. # Commands used pkg_delete ; pkg_add
  10. # Output is interactive, so it writes directly to tty, no stdout
  11. # I run the this demo.rb file like that : doas ./demo.rb
  12. # doas is the replacement of sudo
  13. #
  14. # Commands behavior
  15. # - Get status zero even the command fails
  16. # - stderr is filled on error, so we can filter on this like stderr.empty? to know if there's a failure
  17. # - stdout empty except using system method but can't store stderr
  18. # - i can get stderr (not the full message) using Open3.capture3 but can't get stdout
  19.  
  20. # remove sl package
  21. cmd = 'pkg_delete sl'
  22.  
  23. # add sl package
  24. cmd2 = 'pkg_add sl'
  25.  
  26. # try with a bad package name
  27. cmd3 = 'pkg_add sli'
  28.  
  29.  
  30. # sample of commands
  31. cmd = 'pkg_delete sl'
  32. cmd2 = 'pkg_add sl'
  33. cmd3 = 'pkg_add sli'
  34.  
  35. # using system method, can print stdout but no way to store stderr or stop on error
  36. puts :system_cmd_remove
  37. exit unless system(cmd) => sl-5.02: ok
  38.  
  39. puts :system_cmd2_add
  40. exit unless system(cmd2) => sl-5.02: ok
  41.  
  42. puts :system_cmd3_bad
  43. exit unless system(cmd3) => Cant find sli\nopen3_cmd_remove
  44.  
  45. # Using Open3.capture3, no stdout, can store stderr (not fully)
  46. puts :open3_cmd_remove
  47. stdout, stderr, status = Open3.capture3(cmd)
  48. puts "stdout : #{stdout}" => stdout :
  49. puts "stderr : #{stderr}" => stderr :
  50. puts "status : #{status}" => status : pid 51369 exit 0
  51.  
  52. puts :open_cmd2_add
  53. stdout, stderr, status = Open3.capture3(cmd2)
  54. puts "stdout : #{stdout}" => stdout :
  55. puts "stderr : #{stderr}" => stderr :
  56. puts "status : #{status}" => status : pid 99908 exit 0
  57.  
  58. puts :open_cmd3_bad
  59. stdout, stderr, status = Open3.capture3(cmd3)
  60. puts "stdout : #{stdout}" => stdout :
  61. puts "stderr : #{stderr}" => stderr : Cant find sli
  62. puts "status : #{status}" => status : pid 24159 exit 0
Add Comment
Please, Sign In to add comment