How about using Open3::popen2
?
require 'open3'
Open3.popen2('sudo fdisk -e /dev/disk2') { |i,o,t|
i.puts 'flag 1'
i.puts 'quit'
i.puts 'yes'
i.close
p o.read
t.join
}
Вопрос
On my Mac OS X, I am trying to provide input to fdisk command via echo and Pipe Operator, And in this process i wants to provide bootable flag to partition number 1 of attached USB Drive and then i wants to quit with save, To achieve this I wrote following on Terminal:
xxx-Mac:~xxx$ echo -e 'flag 1\nquit\nyes' | sudo fdisk -e /dev/disk2
I tried it, This works while i type manually on terminal but failed when i put it on script and use them.
I also got Successful Exit status for this command during script execution
I wrote that command on ruby script like this:
#!/usr/bin/env ruby
`echo -e 'flag 1\nquit\nyes' | sudo fdisk -e /dev/disk2`
What is the better way to write this so that command will work successfully wih save all the changes
Edit:
I got follwoing output when i tried by using suggested way on irb:
`echo -e 'flag 1\\nquit\\nyes' | sudo fdisk -e /dev/disk2`
"Enter 'help' for information\nfdisk: 1> Invalid command '-e'. Try 'help'.\nfdisk: 1>"
Решение
How about using Open3::popen2
?
require 'open3'
Open3.popen2('sudo fdisk -e /dev/disk2') { |i,o,t|
i.puts 'flag 1'
i.puts 'quit'
i.puts 'yes'
i.close
p o.read
t.join
}