0

I am trying to store the output of system in a variable.

src_chksum =  'CertUtil -hashfile "C:\Users\Public\Videos\Wildlife.wmv" MD5'
print src_chksum

Output:

CertUtil -hashfile "C:\Users\Public\Videos\Wildlife.wmv" MD5

But the actual output is split across three lines:

MD5 hash of file C:\Users\abhishek.prusty\Desktop\wildlife.wmv:
d8 c2 ea fd 90 c2 66 e1 9a b9 dc ac c4 79 f8 af
CertUtil: -hashfile command completed successfully.

When I use system before the backticks in the above code, only True was returned and stored. Thanks to question 8753691, I removed system and am only using the back ticks; I manage to store one line.

How do I do it when the output is split across multiple lines?

Abhishek Prusty
  • 862
  • 9
  • 15
  • 1
    You do realize that in your code that you are setting `src_chksum` to the string containing your command, as such print it just shows your command without running anything. – B8vrede Feb 14 '16 at 10:30
  • But the backticks are supposed to execute the command and return the output as a string.. or am i wrong ? – Abhishek Prusty Feb 14 '16 at 10:37
  • 1
    http://stackoverflow.com/questions/2232/calling-shell-commands-from-ruby – B8vrede Feb 14 '16 at 10:40

1 Answers1

1

If you need to extract only md5 hash from that output you can use this:

src_chksum = `CertUtil -hashfile "C:\Users\Public\Videos\Wildlife.wmv" MD5`  #make sure you use the backticks instead of single quotation marks

md5_hash = src_chksum.split("\n")[1].gsub(' ', '')
FixerRB
  • 326
  • 2
  • 11