Hi, Depenbding on whether running under windows or unix/mac I obviously need to use the \ instead of the / character for shell commands so I have 2 questions really 1) I would love to know if anyone has a neat way of dealing with converting paths to use ''\'' instead of ''/'' for use with command line calls like command and backtick? 2) How to determine the applications Operating System environment? This is the function I need it for def create_command_line_string(cmd) result = Rails.root.join(cmd) logger.debug("@@@@ - #{result}") result end Any ideas or pointers would be greatly appreciated -- Posted via http://www.ruby-forum.com/.
James West wrote:> Hi, > Depenbding on whether running under windows or unix/mac I obviously need > to use the \ instead of the / character for shell commands so I have 2 > questions really > 1) I would love to know if anyone has a neat way of dealing with > converting paths to use ''\'' instead of ''/'' for use with command line > calls like command and backtick? > > 2) How to determine the applications Operating System environment? > > This is the function I need it for > > def create_command_line_string(cmd) > result = Rails.root.join(cmd) > logger.debug("@@@@ - #{result}") > result > end > > Any ideas or pointers would be greatly appreciatedJust an adendum to this. I''m actually trying to call cap deploy with the result of this function logger.debug("Creating domain") cmd = create_command_line_string("cap cloud_deploy:setup -S destination=#{self.requested_url}") response = `#{cmd}` exit_code = $? logger.debug("@@@@ Create domain response = #{response}, Exit code = #{exit_code}") exit_code This is producing a command "c:/development/cloud/cap cloud_deploy:setup -S etc..." Which is fine on the server as the Rails.root will give me the correct application root path but not on development. In development I obviously need to just replace the / slashes with \ slashes. It would be nice though if the ` backtick usage actually returned something. Anything at all would be good but the output from the logger.debug looks like this @@@@ Create domain response = , Exit code So maybe backticks are not the right solution? Is there something that will give me more info over what is going on so I can deal with results more effectively? -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Sep-04 15:01 UTC
Re: Best way to convert path slash for backtick usage?
James West wrote:> Hi, > Depenbding on whether running under windows or unix/mac I obviously need > to use the \ instead of the / character for shell commands so I have 2 > questions really > 1) I would love to know if anyone has a neat way of dealing with > converting paths to use ''\'' instead of ''/'' for use with command line > calls like command and backtick?path.gsub(''/'', ''\\'') Actually, you may need 4 backslashes instead of 2; Ruby does weird thing with backslashes. But that should get you going. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Markus Roberts
2009-Sep-04 15:08 UTC
Re: Best way to convert path slash for backtick usage?
>> Depenbding on whether running under windows or unix/mac I obviously >> need >> to use the \ instead of the / character for shell commands so I >> have 2 >> questions really >> 1) I would love to know if anyone has a neat way of dealing with >> converting paths to use ''\'' instead of ''/'' for use with command line >> calls like command and backtick?Split it into segments and use File.join()...that''s what it''s there for. -- MarkusQ
Markus Roberts wrote:>>> Depenbding on whether running under windows or unix/mac I obviously >>> need >>> to use the \ instead of the / character for shell commands so I >>> have 2 >>> questions really >>> 1) I would love to know if anyone has a neat way of dealing with >>> converting paths to use ''\'' instead of ''/'' for use with command line >>> calls like command and backtick? > > Split it into segments and use File.join()...that''s what it''s there for. > > -- MarkusQFile.join does exactly the same thing as Rails.root.join so that''s no good at all. But thanks anyway. It was worth a thought. -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote:> James West wrote: >> Hi, >> Depenbding on whether running under windows or unix/mac I obviously need >> to use the \ instead of the / character for shell commands so I have 2 >> questions really >> 1) I would love to know if anyone has a neat way of dealing with >> converting paths to use ''\'' instead of ''/'' for use with command line >> calls like command and backtick? > > path.gsub(''/'', ''\\'') > > Actually, you may need 4 backslashes instead of 2; Ruby does weird thing > with backslashes. But that should get you going. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgpath.gsub would do the job. Thankyou. I need to know how to determine the current operating system in order to decide whether or not to call the gsub routine. Any ideas? I''m thinking that Rails must have a way of dealing with this otherwise it wouldn''t run on Windows. -- Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-04 16:26 UTC
Re: Best way to convert path slash for backtick usage?
In Windows:>> Dir.chdir ''c:/myscripts''=> 0>> Dir.pwd=> "c:/myscripts" I think you''re making a mountain out of a molehill. Ruby interprets the path properly. Keep it to ''/'' and do input sanitation when the user tries to give you a ''\'' Yet, I don''t know what you''re doing, so..>> PLATFORM=> "i386-mswin32" -- Posted via http://www.ruby-forum.com/.
Aldric Giacomoni wrote:> In Windows: >>> Dir.chdir ''c:/myscripts'' > => 0 >>> Dir.pwd > => "c:/myscripts" > > I think you''re making a mountain out of a molehill. Ruby interprets the > path properly. Keep it to ''/'' and do input sanitation when the user > tries to give you a ''\'' > > Yet, I don''t know what you''re doing, so..No mountain, no molehill :-) What I''m doing was clearly (I hope) explained in my 2 opening posts but just to be clear here it is again I''m actually trying to call cap deploy with the result of this function logger.debug("Creating domain") cmd = create_command_line_string("cap cloud_deploy:setup -S destination=#{self.requested_url}") response = `#{cmd}` exit_code = $? logger.debug("@@@@ Create domain response = #{response}, Exit code #{exit_code}") exit_code This is producing a command "c:/development/cloud/cap cloud_deploy:setup -S etc..." Which is wrong on Windows :-)> >>> PLATFORM > => "i386-mswin32"Thank you :-) -- Posted via http://www.ruby-forum.com/.
Rob Biedenharn
2009-Sep-04 17:05 UTC
Re: Best way to convert path slash for backtick usage?
On Sep 4, 2009, at 12:40 PM, James West wrote:> Aldric Giacomoni wrote: >> In Windows: >>>> Dir.chdir ''c:/myscripts'' >> => 0 >>>> Dir.pwd >> => "c:/myscripts" >> >> I think you''re making a mountain out of a molehill. Ruby interprets >> the >> path properly. Keep it to ''/'' and do input sanitation when the user >> tries to give you a ''\'' >> >> Yet, I don''t know what you''re doing, so.. > > No mountain, no molehill :-) > What I''m doing was clearly (I hope) explained in my 2 opening posts > but > just to be clear here it is again > > I''m actually trying to call cap deploy with the result of this > function > > logger.debug("Creating domain") > cmd = create_command_line_string("cap cloud_deploy:setup -S > destination=#{self.requested_url}") > response = `#{cmd}` > exit_code = $? > logger.debug("@@@@ Create domain response = #{response}, Exit > code > #{exit_code}") > exit_code > > This is producing a command "c:/development/cloud/cap > cloud_deploy:setup > -S etc..." > Which is wrong on Windows :-) > >> >>>> PLATFORM >> => "i386-mswin32" > > Thank you :-) > --irb> PLATFORM => "universal-darwin9.0" irb> File::SEPARATOR => "/" irb> File::ALT_SEPARATOR => nil Can you use either of these on Windows to construct a suitable gsub? Guessing: cmd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR Or you can go brute-force with: gsub(''/'',''\\\\'') irb> Dir.pwd => "/Users/rab/code/ruby" irb> Dir.pwd.gsub(''/'',''\\\\'') => "\\Users\\rab\\code\\ruby" irb> puts _ \Users\rab\code\ruby => nil -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
> Guessing: > cmd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR) if > File::ALT_SEPARATOR > > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgYou absolute star :-) That will do EXACTLY what I need. Thanks so much. James -- Posted via http://www.ruby-forum.com/.
Aldric Giacomoni
2009-Sep-04 17:22 UTC
Re: Best way to convert path slash for backtick usage?
>>>>> PLATFORM >>> => "i386-mswin32" >> >> Thank you :-) >> -- > > > irb> PLATFORM > => "universal-darwin9.0" > irb> File::SEPARATOR > => "/" > irb> File::ALT_SEPARATOR > => nilirb(main):060:0> RUBY_DESCRIPTION => "ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]" irb(main):058:0> File::SEPARATOR => "/" irb(main):059:0> File::ALT_SEPARATOR => "\\" Hope that helps. -- Posted via http://www.ruby-forum.com/.