From: ara.t.howard@... Date: 2006-07-29T01:08:02+09:00 Subject: Re: Double quotes embedded in a string ... On Sat, 29 Jul 2006, Dale Martenson wrote: > Here is something that I am struggling with -- I would like to execute a > command string with an IO.popen call that contains embedded double quote > characters. When I build the initial command string everything seems to > work, but once I pass is on to IO.popen, it seems to mess up the double > quotes. > > Here is my example, I want to execute the following command from a Ruby > program: > > pppd connect "chat -v '' ATZ OK ATDT5551234 CONNECT '' ogin: > username word: password" /dev/ttyS0 115200 -detach crtscts modem defaultroute > > I create the command string by the long and somewhat ugly ruby command: > > command = "pppd connect \\\"chat -v '' #{initialization_command} > #{initialization_response} #{dial_command} #{dial_response} '' > #{username_prompt} #{username} #{password_prompt} #{password}\\\" > #{device.name} #{speed} -detach crtscts modem defaultroute\r\n" use this cmd = %W[ pppd connect " chat -v '' #{ initialization_command } #{ initialization_response } #{ dial_command } #{ dial_response } '' #{ username_prompt } #{ username } #{ password_prompt } #{ password } " #{ device.name } #{ speed } -detach crtscts modem defaultroute ].join.strip > In this case, I escaped the escaped the double quote. I have also tried > various combinations of multiple double quotes (4 in a row, etc.). using %W or %Q is the way to go. > If I now echo the command using a system call: > > system( "echo #{command}" ) > > the command seems to echo the desired result: > > pppd connect "chat -v ATZ OK ATD5551234 CONNECT ogin: username > word: password" /dev/ttyS0 115200 -detach crtscts modem defaultroute becuase you running it via the shell - to whom your '' mean something. > But when I pass it to IO.popen, I get you bypass the shell. so you calling the pppd program with the following elements of argv argv[1] = connect argv[2] = "chat argv[3] = -v argv[4] = '' argv[5] = ATZ so there are all sorts of problems - you are losing the word splitting/grouping that sh/bash normally does to your command, so you lose your big double quoted string. you are also losing your empty strings since bash doesn't get to take a whack at those you are passing two single quote marks instead. you can try to build up the command yourself - or you can just let bash/sh do it for you: assume you build the command as above, then just let sh execute it for you like so stdout = IO.popen('sh','r+') do |sh| sh.puts cmd sh.close_write sh.read end > which is caused by that the double quotes are being stripped by > IO.popen. How do I avoid this? just to clarify. it's not that IO.popen stripes double quotes, it's that bash has not interpreted them. regards. -a -- we can never obtain peace in the outer world until we make peace with ourselves. - h.h. the 14th dali lama